Jump to content

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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.