2013-08-16 36 views
6

請考慮以下URL:http://www.myurl.fr/accueilphp/symfony2從URL中隱藏app.php

它不起作用。但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 mod已啓用。我只是不知道還有什麼要檢查。任何想法我失蹤?

編輯 可能是因爲我正在處理它,所以這兩個URL在給定時間都不起作用。我的問題仍然有效。

+0

你可以改變'的AllowOverride All'到'AllowOverride None'。不太可能解決你的問題。 – cheesemacfly

+0

是的,這不會解決我的問題:) – Sam

回答

6

比較你的規則和symfony標準.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> 
+0

確實有效!但我的CSS,圖像和腳本不再被發現。我如何解決這個問題?我想我應該有一個規則來忽略重寫這些類型的文件? – Sam

+0

好的我已經添加了三條規則來忽略css,img和js文件夾,所以它們不會被重寫。我用這3條規則編輯了你的答案,所以我可以將其標記爲答案。非常感謝。 – Sam

+0

@Sam其中是css,img和js文件夾的忽略規則? –

3

修改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> 
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` 

所以,如果你打着頭撞牆,想知道爲什麼沒有什麼工作,試試這個。它可能會保存你的理智和你的項目!快樂的編碼!

+0

謝謝,我發現默認情況下apache沒有啓用mod_rewrite,當我啓用它時,我的配置完美運行 –

+0

很高興我能夠幫助至少一個人! – icarlosmendez

+0

@icarlosmendez - Make that 2. Thanks! – stefgosselin

2

這裏解決問題的關鍵是要通過鍵入

a2enmod rewrite 

確保mod_rewrite的啓用如果你有阿帕奇2.4,然後檢查有代替Order allow.deny

Require all granted 
您的配置文件

這是目錄中的示例配置指令

AllowOverride all 
Require all granted 
0

@icarlosmendez您對「sudo a2enmod rewrite」的建議確實節省了我的一天!

這是我做的,希望有人會發現它有用

  1. 運行「命令a2enmod重寫」第一
  2. symfony複製代碼,將其下的「在/ etc/apache2的/ sites-可用」
  3. 提醒其他人誰得到500錯誤,請檢查項目文件夾下該變種有777
+0

>中的代碼複製symfony中,將其放置在「/ etc/apache2/sites-available「 什麼?這應該不起作用,站點可用是保存apache vhost配置的地方。 – stefgosselin

+0

這就是我的意思。 從[Apache with mod_php/PHP-CGI](http://symfony.com/doc/current/setup/web_server_configuration.html#web-server-apache-mod-php)部分複製第二部分代碼。 – Ben

0

這個代碼添加到你的apache的conf /etc/apache2/sites-available/000-default.conf

和確認mod_rewrite通過鍵入

啓用a2enmod重寫

<VirtualHost *:80> 
     ServerAdmin [email protected] 
     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>