Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Trailing Slash .htaccess

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?

like image 560
Simon Avatar asked Aug 06 '12 13:08

Simon


People also ask

How do you add a trailing slash in htaccess?

How to add a trailing slash to URLs. One of the easiest and most convenient way to add or enforce trailing slashes on URLs is by RewriteRule directive. You can write a single rule in your htaccess file that will apply to all URLs without a trailing slash.

How do I add a trailing slash to my WordPress URL?

To do so, go to the “Settings -> Permalinks” section and, depending on your situation, either add or delete the last slash. The “Custom Structure” field ends with a slash, so all other WordPress URLs will have the trailing slash. Likewise, if it is not present there, the slash will be missing in your website's URLs.

How do I get rid of trailing slash?

Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.


4 Answers

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

like image 80
Simon Avatar answered Oct 04 '22 17:10

Simon


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]
like image 22
Pooja Gupta Avatar answered Oct 04 '22 16:10

Pooja Gupta


Please use this format in .htaccess,

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
like image 30
Baskar Vel Avatar answered Oct 04 '22 16:10

Baskar Vel


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.

like image 23
Gerfried Avatar answered Oct 04 '22 16:10

Gerfried