I'm trying to remove the last slash of my URLs.
I put this in my htaccess file :
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
It works with files hosted in the root (or pages generated from the database), but it doesn't work with files hosted in subdirectories.
For example, I would like that this :
http://mywebsite.com/dir1/dir2/dir3/index.php
goes to that :
http://mywebsite.com/dir1/dir2/dir3
but it goes to that :
http://mywebsite.com/[homeserver]/[server]/www/dir1/dir2/dir3
What is wrong with my htaccess file ?
Edit : here is the entire htaccess file
SetEnv PHP_VER 5
SetEnv REGISTER_GLOBALS 0
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
### Remove www
RewriteCond %{HTTP_HOST} !^mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R=301,L]
### Get the URL in pathinfo mode
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1
### Redirect index.php to the root
RewriteRule ^index\.php$ http://mywebsite.com/ [R=301,L]
Try to add add below rule after RewriteEngine
On and provide your full .htaccess
for further analysis.
RewriteBase /
Have it like this:
SetEnv PHP_VER 5
SetEnv REGISTER_GLOBALS 0
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
### Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]
### Redirect index.php from anywhere
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
### Get the URL in pathinfo mode
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L]
Make sure to clear your browser cache before testing this change.
You can use below code in .htacccess
file to remove slash at the end of URL.
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
I have used this and working fine.