簡體   English   中英

如何使用htaccess從特定的URL刪除正斜杠

[英]how to remove forward slash from specific url using htaccess

我有以下.htaccessfile

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

我有以下網址:

http://localhost/mysite/loginPage/

我想確保此網址永遠不會以正斜杠(loginPage /)結尾。

我嘗試使用以下幾行:

RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^([^/]+/[0-9]+/[0-9]+/[0-9]+/[^/]+)/.+$ - [L,R=404]

但是在所有情況下,頁面都無法正確重定向時,我都會收到錯誤消息。 請記住,我只需要上面的特定網址,而不用擔心網站的其余部分。

這取決於您將重寫規則放在何處。 您提供的示例確實有用。 但是您必須將它們放在最后一個htaccess行之前。

RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

這將創建正確的輸出,並且可以通過htaccess測試儀之類的東西進行測試

如果您只想要該URL,則可以使用:

RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/loginPage/$ mysite/loginPage? [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

您也可以使用

RewriteRule ^mysite/loginPage/$ http://%{HTTP_HOST}/mysite/loginPage? [R=301,L]

我只想要上述特定網址

在這種情況下,您的規則應僅與此URI專門匹配,而不是.*/.*等。

有這樣的規則:

RewriteEngine on

RewriteCond %{REQUEST_URI} ^(/mysite/loginPage)/$ [NC]
RewriteRule ^ %1 [L,R=301]

RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php/$0 [L]

您實際上也不需要在最后一條規則中使用QSA標志。

在測試此更改之前,請確保清除瀏覽器緩存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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