Jump to content

Remove trailing slash using htaccess


deluxes

Recommended Posts

So I'm trying to remove the trailing slash from my URLs using .htaccess

For example, 

 

mywebsite.com/mypage/ to become mywebsite.com/mypage

 

After a quick browse online I found two possible solutions, however I can't seem to get either to work properly. 

 

The first causes an error "The requested URL /home/sites/mywebsite.com/public_html/mypage was not found on this server."

 



RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]


 

 

The second method I found causes a 'redirect loop' error on my site.



RewriteRule ^(.*)/$ /$1 [R,L]


 

Please can someone give me some advice?

 

Many thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/296638-remove-trailing-slash-using-htaccess/
Share on other sites

Hi, mypage is a directory.

 

 

Here is my .htaccess file, excluding the above rules, would anything in the below conflict with such rules? I don't really understand .htaccess! (domain = my own domain, undisclosed)

 

 
Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^domain.co.uk$ [NC] 
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
RewriteRule ^index\.html?$ / [NC,R,L]
 
# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
 
RewriteCond %{ENV:HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Apache needs the slash if you want it to serve whatever DirectoryIndex file (eg, index.php) automatically. If you want to turn the slash off then you have to (a) add it back yourself or (b) do the work of serving the index.php yourself.

 

# do not automatically append slashes to directories
DirectorySlash off

# do automatically use index.php
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*[^/]$ $0/index.php [L]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.