简体   繁体   中英

how to remove forward slash from specific url using htaccess

I have the following .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/).

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.

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

If you want it specifically for just that url you could resort to:

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.

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.

Make sure to clear your browser cache before testing this change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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