1

I'm having a little trouble with my site's .htaccess file. I want to have both the page.html and page/ URLs redirect to page without any suffix. I've been able to get the .html off, but when entering the page/ URL I get an internal server error no matter what I try (Apache 2.4.18).

Here's the full .htaccess code (without any of the attempts to remove the trailing slash)

Options +FollowSymLinks -Indexes -MultiViews

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.website.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

What I'd like to do is just copy the last 6 lines that worked for .html but have it act on the trailing slash instead, but I'm not sure how to do that or if there's a better solution. Any advice is greatly appreciated!

L. Thane
  • 83
  • 9

2 Answers2

1

To remove the trailing slash do this:

RewriteEngine on
RewriteRule (.+)/$ /example/$1 [L,R=301]

To remove excess slashes:

RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R]
Joe
  • 4,877
  • 5
  • 30
  • 51
  • Thanks thickguru, this seems to work! Previously I'd tried the same or similar code under the .html redirect, but putting it above it seems to have everything working fine. Greatly appreciated! – L. Thane May 25 '16 at 18:12
  • One weird thing is that when I type in mydomain.com/page/ it redirects to mydomain.com//page, and then when I enter mydomain.com/page/ again it redirects to mydomain.com/page, and switches between the two slash and one slash versions every time. Any idea if that's fixable? Not a big deal since both work, but would be nice to keep it clean. – L. Thane May 25 '16 at 18:19
  • Hey @L.Thane, that is strange... you could try adding an extra slash to the rewriterule, but I'm not sure if that would work. I've edited my answer with a rule that is supposed to get rid of excess slashes. – Joe May 25 '16 at 18:32
  • Thanks, the new RewriteCond is able to get rid of the double slash on refresh, but can't seem to prevent it from occurring in the first place no matter where it's placed in the code. Playing around with the number of slashes in the RewriteRule hasn't had any effect either, very strange. – L. Thane May 25 '16 at 18:47
  • You could try using `RewriteBase /` Other than that I'm not too sure I'm afraid.. :/ – Joe May 25 '16 at 18:49
  • Bizarre, it seems to be fine now - I'm using `RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.+)/$ /$1 [L,R=301]` Not sure why it works, since I'm 80% sure I tested the exact same thing 5 minutes ago and it wasn't working, but it's good now. Thanks for your help! – L. Thane May 25 '16 at 18:50
  • Mad lol... I'm glad it is now working! Happy I could help with the first part! :) – Joe May 25 '16 at 18:51
0

Have you tried something like

(\.html|\/)

I'm not on my mac right now, but you could do it with or operator.

Check it on https://regex101.com/ with multiple urls.

Tob
  • 627
  • 6
  • 9
  • Thanks Tobias, I tried replacing the \.html\ with that, but it seemed to cause all URLs to go to an internal server error. Does it need to go in other locations as well? – L. Thane May 25 '16 at 18:10