【问题标题】:php/symfony2 hiding app.php from the URLphp/symfony2 从 URL 中隐藏 app.php
【发布时间】:2013-08-19 08:13:17
【问题描述】:

考虑以下 URL:http://www.myurl.fr/accueil

这行不通。但是http://www.myrurl.fr/app.php/accueil 确实有效。

我摆脱了 .htaccess 文件,因为我想使用 vhost 文件并依赖 Apache 路由。我的虚拟主机文件如下:

<VirtualHost my.ip.address>
ServerName myurl.fr
ServerAlias www.myurl.fr
DocumentRoot /var/www/mysite/web
DirectoryIndex app.php
<Directory "/var/www/mysite/web">
  AllowOverride All
  Allow from All
</Directory>

<IfModule mod_rewrite.c>
 RewriteEngine On  
 RewriteCond %{ENV:REDIRECT_STATUS} ^$  
 RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
 RewriteRule .? - [L]
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteRule ^(.*)$ app.php [QSA,L]
 RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
 RewriteRule ^(.*) - [E=BASE:%1]    
 RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>
</VirtualHost>

我已经清除了 Apache 缓存并重新启动了很多次。启用了 urlrewrite 模块。我只是不知道还要检查什么。知道我错过了什么吗?

编辑 由于我正在处理它,这可能是两个 URL 在给定时间都不起作用。我的问题仍然有效。

【问题讨论】:

  • 您可以将AllowOverride All 更改为AllowOverride None。不太可能解决您的问题。
  • 是的,这不会解决我的问题 :)

标签: php apache symfony url-rewriting


【解决方案1】:

将此代码添加到您的 apache conf /etc/apache2/sites-available/000-default.conf

并确保通过键入启用 mod_rewrite

a2enmod 重写

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/web/
    DocumentRoot /var/www/html/web
    <Directory /var/www/html/web>
        AllowOverride None
        Order Allow,Deny
        Allow from All
        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app.php [QSA,L]
        </IfModule>
    </Directory>
</VirtualHost>

【讨论】:

    【解决方案2】:

    @icarlosmendez 您对“sudo a2enmod rewrite”的建议真的节省了我的时间!

    这就是我所做的,希望对某些人有帮助

    1. 先运行“sudo a2enmod rewrite”
    2. symfony复制代码,放在“/etc/apache2/sites-available”下
    3. 只是给遇到 500 个错误的人的说明,请检查项目文件夹下的 var 是否有 777。

    【讨论】:

    • > 从 symfony 复制代码,放在“/etc/apache2/sites-available”下什么?这应该不起作用,站点可用是保留 apache vhost 配置的位置。
    • 这就是我的意思。从Apache with mod_php/PHP-CGI 部分复制代码的第二部分。
    【解决方案3】:

    这里的关键解决方案是确保通过键入

    启用 mod_rewrite
    a2enmod rewrite
    

    如果你有 apache 2.4,然后检查你的配置文件,而不是 Order allow.deny

    Require all granted
    

    这是 directory 指令中的示例配置

    AllowOverride all
    Require all granted
    

    【讨论】:

      【解决方案4】:

      Symfony 文档为这个问题提供了直接的解决方案。

      '<VirtualHost *:80>
          ServerName domain.tld
          ServerAlias www.domain.tld
      
          DocumentRoot /var/www/project/web
          <Directory /var/www/project/web>
              AllowOverride None
              Order Allow,Deny
              Allow from All
      
              <IfModule mod_rewrite.c>
                  Options -MultiViews
                  RewriteEngine On
                  RewriteCond %{REQUEST_FILENAME} !-f
                  RewriteRule ^(.*)$ app.php [QSA,L]
              </IfModule>
          </Directory>
      
          # uncomment the following lines if you install assets as symlinks
          # or run into problems when compiling LESS/Sass/CoffeScript assets
          # <Directory /var/www/project>
          #     Options FollowSymlinks
          # </Directory>
      
          # optionally disable the RewriteEngine for the asset directories
          # which will allow apache to simply reply with a 404 when files are
          # not found instead of passing the request into the full symfony stack
          <Directory /var/www/project/web/bundles>
              <IfModule mod_rewrite.c>
                  RewriteEngine Off
              </IfModule>
          </Directory>
          ErrorLog /var/log/apache2/project_error.log
          CustomLog /var/log/apache2/project_access.log combined
      </VirtualHost>'
      

      我想在这次讨论中补充一点是,如果没有以下考虑,这些优秀的建议都不会取得成果。

      如果您使用的是 Apache,则必须确保启用 mod_rewrite!

      `sudo a2enmod rewrite`
      

      因此,如果您正在用头撞墙,想知道为什么没有任何效果,请试试这个。它可能会拯救你的理智和你的项目!编码愉快!

      【讨论】:

      • 谢谢,我发现apache默认没有启用mod_rewrite,当我启用它时,我的配置工作正常
      • 很高兴我能够帮助至少一个人!
      • @icarlosmendez - 制作 2。谢谢!
      • @icarlosmendez 谢谢.. 您的评论“如果您使用 Apache,则必须确保启用 mod_rewrite!sudo a2enmod rewrite”对我有帮助
      【解决方案5】:

      把 apache httpd.conf 虚拟主机代码改成这个

      ServerName my-site.fr
      <VirtualHost your.ip.address:80>
      DocumentRoot /var/www/mysite/web
      <Directory "/var/www/mysite/web">
      DirectoryIndex app.php
      Options -Indexes FollowSymLinks SymLinksifOwnerMatch
      AllowOverride All
      Allow from All
      </Directory>
      </VirtualHost>
      

      【讨论】:

      • 这个语法给了我一个 apache 错误。更好地使用已开发的语法,逐行执行一条指令。就像它在 symfony 的文档上一样 symfony.com/doc/current/setup/…
      • 是的,我的回答有点过时了,它已经 5 岁了,现在你可以拥有我们当时没有的非官方 apache 配置
      【解决方案6】:

      将您的规则与 symfony-standard .htaccess 进行比较,我发现您在“通过所有规则”之前错过了文件检查。试试看

      <IfModule mod_rewrite.c>
          RewriteEngine On  
      
          RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
          RewriteRule ^(.*) - [E=BASE:%1]
          RewriteCond %{ENV:REDIRECT_STATUS} ^$
          RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
          # If the requested filename exists, simply serve it.
          # We only want to let Apache serve files and not directories.
          RewriteCond %{REQUEST_FILENAME} -f
          RewriteRule .? - [L]
      
          # Rewrite all other queries to the front controller.
          RewriteRule .? %{ENV:BASE}/app.php [L]
      </IfModule>
      

      【讨论】:

      • 确实有效!但是我的 css、图像和脚本不再被发现。我该如何解决?我想我应该有一个规则来忽略这些类型的文件的重写?
      • 好的,我添加了三个规则来忽略 css、img 和 js 文件夹,这样它们就不会被重写。我已经使用这 3 条规则编辑了您的答案,因此我可以将其标记为答案。非常感谢。
      • @Sam css、img 和 js 文件夹的忽略规则在哪里?
      猜你喜欢
      • 2013-05-17
      • 2013-08-25
      • 1970-01-01
      • 2016-03-31
      • 2017-01-10
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多