简体   繁体   中英

How to remove a trailing slash from the URL via the rewrite engine?

In the root directory of my web-site I have a .htaccess file with the following contents:

RewriteEngine On        
RewriteRule ^blogs$ ./blogs.php

As a result, when a visitor requests the URL mysite.com/blogs , the PHP page mysite.com/blogs.php is processed and returned.

This behavior works fine. I would like to enhance it with this additional behavior: If the visitor requests the URL mysite.com/blogs/ (notice the trailing slash), I would like him to be redirected tho the URL mysite.com/blogs . (I believe a redirect is necessary, since I don't want the trailing slash to appear in the address bar in the visitor's browser). Then, my above mentioned rewrite would kick in and blogs.php would be processed.

I have tried:

RewriteEngine On 
RewriteRule ^blogs/$ ./blogs [R=301]
RewriteRule ^blogs$ ./blogs.php

but that results in the URL changing to http://mysite.loc/E:/Projects/mysite/blogs without even redirecting. (I'm testing on local-host. E:/Projects/mysite is the root.)

How do I make this work?

I've figured it out :-)

The problem was the leading dot. I've removed it and now it works.

RewriteRule ^blogs/$ /blogs [R=301]
RewriteRule ^blogs$ ./blogs.php

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