[英]htaccess redirect to remove index.php
我希望用戶能夠使用$_SERVER['PATH_INFO']
作為占位符,為哪個文件提供服務並擺脫地址欄中的index.php
例如,我想將me.com/index.php/settings1
作為me.com/settings1
提供給用戶使用的任何PATH_INFO等等。
我怎么寫這個作為htaccess規則? 我甚至不知道從哪里開始
如果它有幫助,我正在使用hostgator的共享主機方案。
根據@EddyFreddy的建議,我嘗試了在這個問題中提到的兩種方法,沒有運氣。 這是目前文件的樣子:
suPHP_ConfigPath /home/user/php.ini
<Files php.ini>
order allow,deny
deny from all
</Files>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule .? http://mysite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L] # tried with and without the `?`
這可以通過mod_rewrite輕松處理。 在DOCUMENT_ROOT下的.htaccess中使用此代碼:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /index.php/foo to /foo/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1/ [L,R]
# external redirect to add trailing slash
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+((?!.+/[\s\?])[^\s\?]+) [NC]
RewriteRule ^ %1/ [L,R]
# internal forward from /foo to /index.php/foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L,NC]
驗證一切正常后,將R
更改為R=301
。
經過長期評估后,我發現anubhava的版本在所有情況下對我都不起作用。 所以我試過這個修改過的版本。 它將處理現有dirs中的現有文件,並且不會產生雙斜線。
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
###### Add trailing slash (optional) ######
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ $1/ [R=301,L]
###### external redirect from /index.php/foo to /foo ######
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php(/.+)?[\s\?] [NC]
RewriteRule ^ %1 [L,R=301]
###### internal forward from /foo to /index.php/foo ######
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php%{REQUEST_URI} [L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.