0

Unfortunately I have a problem where I get stuck.

I would add behind each URL a trailing slash and then redirect the version without a slash via 301 to the variant with a slash.

Previously, i have the html file extension with url rewriting successfully removed and SSL enforced.

So, i tried the following code to add a slash.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

I have found this solution, on this thread: Htaccess: add/remove trailing slash from URL

It remains unfortunately ineffective.

Interestingly, when I, for example, visits the following URL (with a slash at the end):

example.com/imprint/

I get the following message: The requested URL was not found /imprint.html/ on this server.

imprint.html???

Here is my complete .htaccess file:

RewriteEngine On

#Activate and force ssl and redirect from non-www to www#
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]

#Removes HTMl-Extension and make URLs clean# 

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

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

#Redirect Index Files e.g. /index or /index.html
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]

#Adds a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

I hope, you can help me. :-)

Community
  • 1
  • 1
hazelnut
  • 7
  • 5

1 Answers1

0

When you check !-f (not a file) it will skip .html files also. You can use [OR]

RewriteEngine On

#Activate and force ssl and redirect from non-www to www#
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*?)/?$ https://www.example.com/$1/ [L,R=301]

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.+)\.html/?$ /$1/ [R=301,L]

#Redirect Index Files e.g. /index or /index.html
RewriteRule ^index\.html/?$ / [R=301,L]

RewriteRule ^(.+)/index\.html/?$ /$1/ [R=301,L,NE,NE]

#Adds a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301,NE]

#Removes HTMl-Extension and make URLs clean# 

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643