簡體   English   中英

.htaccess 刪除 PHP 擴展與重定向尾部斜杠的沖突

[英].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]
  1. 第一個從 URL 欄中刪除 .php 擴展名
  2. 第三個顯示目錄的內容。 例如example.com/billgates/顯示的頁面內容為: example.com/profile/index.php?username=billgates
  3. 第二個添加尾部斜杠並重定向#2 的非尾部斜杠。 例如, 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM