简体   繁体   English

如何使用htaccess从特定的URL删除正斜杠

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

I have the following .htaccessfile 我有以下.htaccessfile

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

And I have the following url: 我有以下网址:

http://localhost/mysite/loginPage/

I want to ensure that this url will never ends with forward slash (loginPage/). 我想确保此网址永远不会以正斜杠(loginPage /)结尾。

I tried to use the following lines: 我尝试使用以下几行:

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

But I get error message in all cases that page is not redirecting properly. 但是在所有情况下,页面都无法正确重定向时,我都会收到错误消息。 Please have in mind that I want this just for the specific URL above, i don't care for the rest of the site. 请记住,我只需要上面的特定网址,而不用担心网站的其余部分。

It depends on where you put the rule for your rewrite. 这取决于您将重写规则放在何处。 The examples you provided do work. 您提供的示例确实有用。 But you have to put them before your last htaccess line. 但是您必须将它们放在最后一个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]

This will create the proper output, and can be tested through something like a htaccess tester 这将创建正确的输出,并且可以通过htaccess测试仪之类的东西进行测试

If you want it specifically for just that url you could resort to: 如果您只想要该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]

You could also specify an absolute redirect with 您也可以使用

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

I want this just for the specific URL above 我只想要上述特定网址

In that case, your rule should just match specifically this URI rather than .*/.* etc. 在这种情况下,您的规则应仅与此URI专门匹配,而不是.*/.*等。

Have your rules like this: 有这样的规则:

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]

You don't really need QSA flag in last rule also. 您实际上也不需要在最后一条规则中使用QSA标志。

Make sure to clear your browser cache before testing this change. 在测试此更改之前,请确保清除浏览器缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM