deluxes Posted June 4, 2015 Share Posted June 4, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296638-remove-trailing-slash-using-htaccess/ Share on other sites More sharing options...
requinix Posted June 5, 2015 Share Posted June 5, 2015 Is mypage a directory? If not, Apache won't automatically add slashes to files so you'd have to have something that's adding the slashes. Quote Link to comment https://forums.phpfreaks.com/topic/296638-remove-trailing-slash-using-htaccess/#findComment-1513222 Share on other sites More sharing options...
deluxes Posted June 8, 2015 Author Share Posted June 8, 2015 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] Quote Link to comment https://forums.phpfreaks.com/topic/296638-remove-trailing-slash-using-htaccess/#findComment-1513445 Share on other sites More sharing options...
requinix Posted June 8, 2015 Share Posted June 8, 2015 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] Quote Link to comment https://forums.phpfreaks.com/topic/296638-remove-trailing-slash-using-htaccess/#findComment-1513491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.