[英].htaccess remove PHP extension conflicts with redirect trailing slash
我有三段代碼。
這是第一個:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
第二個:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R,NE]
第三個:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-_]+)/$ profile/index.php?username=$1 [QSA,L]
example.com/billgates/
顯示的頁面內容為: example.com/profile/index.php?username=billgates
example.com/billgates
重定向到example.com/billgates/
。所有代碼都可以工作,但不能很好地結合在一起。 這是問題所在:
如果我訪問example.com/login
它會刪除 .php 擴展名並且它可以工作。 但問題是,如果我在/login/
的末尾手動添加一個斜杠,它會重定向到: example.com/login/.php/
但它應該將我重定向到/login
因為它不是一個目錄而只是一個頁。 但是如果/login/
是一個目錄,那么它不應該重定向。
完整的.htaccess:
RewriteEngine On
ErrorDocument 404 /404.php
...the three pieces of codes above
這樣做並在清除瀏覽器緩存后進行測試:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule /$ - [L,R=404]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ profile/index.php?username=$1 [QSA,L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.