Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess with or without slash

Tags:

What do I need to do to the following rewrite rule to make it so it works whether or not their is a slash at the end of the URL?

ie. http://mydomain.com/content/featured or http://mydomain.com/content/featured/

RewriteRule ^content/featured/ /content/today.html 
like image 266
sbuck Avatar asked Feb 12 '09 08:02

sbuck


People also ask

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.

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.

What is a trailing slash?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.


2 Answers

Use the $ to mark the end of the string and the ? to mark the preceding expression to be repeated zero or one times:

RewriteRule ^content/featured/?$ content/today.html

But I recommend you to stick to one notation and correct misspelled:

# remove trailing slashes
RewriteRule (.*)/$ $1 [L,R=301]

# add trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ $0/ [L,R=301]
like image 152
Gumbo Avatar answered Sep 19 '22 15:09

Gumbo


simple way to do this :

RewriteEngine On
RewriteBase / 
RewriteRule ^content/featured(\/||)$ /content/today.html [L,R=301,NC] 
like image 36
M Rostami Avatar answered Sep 16 '22 15:09

M Rostami