[英]Remove 'index.php' from URL with .htaccess
我一直在嘗試從該站點進行的各種解決方案,但是似乎都沒有用。 我目前正在與hostgator托管。 這是我當前的.htaccess文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/user/php.ini
<Files php.ini>
order allow,deny
deny from all
</Files>
</IfModule>
這是在我網站的根文件夾中。 我也嘗試添加一個?
在index.php
之后,沒有運氣。 有人知道為什么這行不通嗎?
這是您可以在.htaccess中使用的代碼(在DOCUMENT_ROOT下),用於從URI中刪除index.php:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Symfony 2具有出色的解決方案:
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
這樣可以完成以下任務:
RewriteBase
(當站點位於Web根目錄下的子目錄中時很有用) index.php
index.php
注意這一行:
RewriteRule ^(.*) - [E=BASE:%1]
負責為以后設置%{ENV:BASE}
變量。 請參閱有關E|env
標志的Apache文檔 。
我嘗試了這個並且工作正常:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
例外情況
如果您站點的系統目錄(/ system /)已重命名並且仍然可以通過URL訪問,請修改上面的RewriteCond行:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
如果您從子目錄而不是域的根目錄(例如, http : //example.com/myeesite/而不是http://example.com/ )運行EE,則只需在索引中刪除index.php之前的斜杠即可。上面的RewriteRule行,如下所示:
RewriteRule ^(.*)$ index.php/$1 [L]
如果您正在從子目錄運行EE,但在刪除斜杠后仍無法使用EE,則可能需要在重寫規則中指定子目錄。 例如,如果您的子文件夾名為test,請更改:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
至:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
並更改:
RewriteRule ^(.*)$ /index.php/$1 [L]
至:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
要從apache2.4上的網址中刪除index.php ,可以使用以下規則:
RewriteEngine on
RewriteRule ^index\.php/(.+)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]
這將改變你的uri
/index.php/foobar
至
/foobar
對於較低版本的apache,此規則將返回內部服務器錯誤,因為它們不支持END標志。 請查看上面適用於幾乎所有版本的anubhava答案。
更新的答案
這些天,我只使用Wordpress .htaccess
作為基礎:它相對簡單,並且可以按預期工作:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
如果您願意,也可以直接強制使用https://
:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
2012年的原始帖子
我最初的建議是先進行硬重定向(剝離index.php
),然后進行內部重定向回到index.php
。
問題是貪婪的內部重定向匹配(例如使用(.*)
)似乎再次觸發了硬重定向,從而導致無限重定向循環。
我覺得有一個比我建議的方案更優雅的解決方案,但是不幸的是,我沒有Apache的專業知識來思考它。
我要做的是擁有一個RewriteRule
,它執行硬重定向以index.php
,然后執行一組內部重定向規則以從那里獲取它。 例如:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php(.*)$ /$1 [R,L,QSA]
RewriteRule ^pie$ /index.php?pie [L,QSA]
</IfModule>
這似乎確實可以正常工作,而不會產生任何500個錯誤。 但是,我再次推薦一點鹽。
盡管這與好的解決方案相去甚遠,但我希望它能有所幫助。
今天,我被迫加入stackoverflow.com在這里發表評論,因為答案已經解決了我在Zend Framework網站上遇到的長期問題。 我在網站上工作了大約3 1/2年,在那段時間里,我發現它無法正確處理index.php,這導致網站站長工具看到重復的標題等。
我決定今天再次嘗試尋找解決方案,這是許多次嘗試中的一次。
這是解決我的問題的一個(上面顯示的最后一個答案)。
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
舉個例子。 這就是它為我實現的。
http://www.domainname.co.uk/index.php/categories/url-name.html
變成
http://www.domainname.co.uk/categories/url-name.html
感謝所有對原始問題做出貢獻的人,因為它導致了上面的答案和解決方案。
額外注意:我還有其他重寫命令可以處理其他方面,但是它們本身並不能修復index.php,並且只有通過使用才能解決此問題。
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
我只希望將來對它的ExpressionEngine或Zend Framework有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.