21

I'm trying to get the following effect (using this local file http://localhost/[company_name]/[project_name]/.htaccess):

http://localhost/[company_name]/[project_name]/page-1 (adds slash)
http://localhost/[company_name]/[project_name]/page-1/ (does nothing)
http://localhost/[company_name]/[project_name]/page-1/subpage-1 (adds slash)
http://www.example.com/page-1 (adds slash)<br />
http://www.example.com/page-1/ (does nothing)
etc.

The thing I want to accomplish is that this .htaccess doesn't need the path http://localhost/[company_name]/[project_name]/ anymore so that I don't have to edit this each time it's been uploaded.

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

I found the code above here: Add Trailing Slash to URLs, but it only makes it possible to use the HOST dynamically and discards the path. Does someone a solution to accomplish this effect?

Community
  • 1
  • 1
Simon
  • 5,464
  • 6
  • 49
  • 85

4 Answers4

64
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 

This code needs to be put at the top of your .htaccess file below RewriteEngine On

Simon
  • 5,464
  • 6
  • 49
  • 85
  • 2
    Don't think this works to turn www.mydomain.com into www.mydomain.com/ – jeffkee Jul 17 '13 at 01:06
  • 1
    The root domain is not included. on the webserver the http://www.mydomain.com and http://www.mydomain.com/ have exactly the same URI. For SEO reasons, they are treated as the same for the root domain, with or without trailing slashes – maskie Aug 05 '13 at 03:05
  • 2
    add ^$ in condition to avoid '/' at the end of domain RewriteCond %{REQUEST_URI} !(/$|\.|^$) RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] – mukund Dec 17 '13 at 06:12
  • This code needs to be put at the top of your .htaccess file below RewriteEngine On – James Dec 31 '19 at 13:52
  • Please mention in answer that it will work when you put these lines on top of .htaccess file. – Shakir Blouch Dec 31 '20 at 19:30
  • Is this working in 2021? I tested on firefox and it's not working. I have a www.test.com/product and it should be displayed like www.test.com/product/ but it's not working – user9437856 Jun 14 '21 at 08:30
  • @user9437856 This worked fine for me now. WP site acting up randomly not redirecting to trailing slash. – Michael Rogers Jun 23 '21 at 16:58
  • How can we exclude GET requests? – jax Dec 12 '21 at 16:16
  • This should be ``RewriteCond %{REQUEST_URI} !(/$|\.) `` to ``RewriteCond %{REQUEST_URI} !(\/$|\.)`` . One trailing was missing. – Can Sep 20 '22 at 22:45
2

Please add below lines top of .htaccess file

RewriteEngine on
#Add Trailing slash for end of the URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R]
1

Please use this format in .htaccess,

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
M_S_N
  • 2,764
  • 1
  • 17
  • 38
Baskar Vel
  • 11
  • 2
1

The problem with all other answers: POST requests lose their data when redirected. You can instruct the browser to resend the POST data by using http 307:

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

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307

Btw I prefer the REQUEST_FILENAME condition. This makes sure that the request really targets a folder structure and not appends the slash to file Urls.

ESP32
  • 8,089
  • 2
  • 40
  • 61