Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Trailing Slash to URLs

There are quite a few results for add trailing slash .htaccess on Google, but all examples I found require the use of your domain name, as in this example:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

My problem is that a hard-coded domain name will not work on my local development machine. Is there a way to add trailing slashes without explicitly telling mod_rewrite the domain name?

like image 535
Karen Avatar asked Dec 29 '22 12:12

Karen


2 Answers

You don’t need to specify the domain, you can simply use an absolute URL path:

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

That does also make a check for the URL scheme obsolete.

like image 195
Gumbo Avatar answered Jan 11 '23 17:01

Gumbo


This should work:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
like image 33
Siddhartha Reddy Avatar answered Jan 11 '23 15:01

Siddhartha Reddy