L. Thane
L. Thane

Reputation: 83

Redirect .html and trailing slash

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!

Upvotes: 1

Views: 1594

Answers (2)

Joe
Joe

Reputation: 4917

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]

Upvotes: 1

Tob
Tob

Reputation: 943

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.

Upvotes: 0

Related Questions