Wouter den Ouden
Wouter den Ouden

Reputation: 1523

.htaccess remove trailing slash

I have a htaccess file for a site to let it's script work . I don't know much about how it works, but I have to remove the trailing slash at the end.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

ErrorDocument 404 https://www.onlinegames.nl/

## 301 Redirects
# 301 Redirect 1
RewriteCond %{HTTP_HOST}  ^www\.onlinegames\.nl$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteCond %{HTTPS} =on
RewriteRule ^index\.html$ https://www.onlinegames.nl/? [R=301,NE,NC,L]

<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>

How can I remove the trailing slash at the end? I've tried to change a few things, but most of the times the script is not working correctly anymore and all the subpages are not working anymore.

Upvotes: 2

Views: 64

Answers (1)

anubhava
anubhava

Reputation: 786091

You may use these rules in your site root .htaccess to replace your shown code. Note that ordering of these rules is also important and change in ErrorDocument.

ErrorDocument 404 /

RewriteEngine On

# remove www from host names
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# redirect http to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]

# strip /index.html
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.html$ / [R=301,NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]

Upvotes: 2

Related Questions