我將symfony應用程序部署到生產環境中,我已將symfony應用程序放在根文件夾 /var/www/html中,並將apache2根目錄設置爲/ var/www/html /但是.htaccess文件中的DirectoryIndex沒有重定向到文件app.phpSymfony .htaccess目錄索引不起作用
在url xxx.xxx.xx.xx /我得到/ var/www/html/web中所有文檔的列表文件夾,如果我做xxx.xxx.xx.xx/app.php應用程序加載。
我不明白如何讓.htaccess中的DirectoryIndex工作。
我一直在經歷下面的堆棧問題類型,但他們不爲我工作:
- Redirect all requests to web/app.php in Symfony
- Symfony: htaccess to hide app.php or app_dev.php
- symfony2 rewrite rules .htaccess app.php
另一篇文章建議設置apache2根目錄作爲/ var/html而不是/ var/html/web,但這也不起作用。因爲url xxxx.xxx.xx.xx/deplays目錄/ var/html
與我最接近的解決方案是將文件app.php重命名爲index.php,它獲得了根url xxx.xxx.xx.xx /加載,但隨後所有'sub'url(如xxxx.xxx.xx.xx/offer/9/Alternance_Apprentissage_graphiste_chez_Eggs)都會失敗,並顯示404 Not Found錯誤。 這是由於.htaccess文件沒有正確重定向url,因爲url xxxx.xxx.xx.xx/app.php/offer/9/Alternance_Apprentissage_graphiste_chez_Eggs正常工作。
我的.htaccess文件看起來像這樣(它是默認生成的文件):
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
任何幫助表示讚賞。這很關鍵,因爲我無法讓我的應用程序繼續生產。
UPDATE
@shevron是對的。從他的回答中可以看出.htaccess文件根本不會被你的Apache安裝程序解析。「和「檢查你的主Apache配置是否在該目錄下的AllowOverride上,嘗試將它設置爲AllowOverride All並重啓你的Apache - 看看是否有效。」
我做了以下內容:
設置我的Apache配置如下:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/web
<Directory "/var/www/html/web">
AllowOverride All
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
這沒有奏效。 @shevron是不是我寫得不好?
但進一步的,我把所有的配置從.htaccess文件到Apache主配置本身和本作的重定向工作:
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/web
<Directory "/var/www/html/web">
AllowOverride All
Allow from All
</Directory>
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
所以現在重定向工作。
感謝您的回答,您對我的.htaccess文件未被解析的事實是正確的。所以,當我把所有配置放在主Apache配置中時,我得到了重定向的工作,但是有一個小問題,重定向不是'無聲的',這意味着'app.php'現在出現在URL中!任何想法如何我可以刪除?如果解決了,那麼你將解決我的問題! –
我建議您閱讀Apache URL重寫指南,如果您仍然無法解決此問題,請發佈一個新問題 – shevron