Htaccess: Add/Remove Trailing Slash from Url

Htaccess: add/remove trailing slash from URL

Right below the RewriteEngine On line, add:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]

to enforce a no-trailing-slash policy.

To enforce a trailing-slash policy:

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

EDIT: commented the R=301 parts because, as explained in a comment:

Be careful with that R=301! Having it there makes many browsers cache the .htaccess-file indefinitely: It somehow becomes irreversible if you can't clear the browser-cache on all machines that opened it. When testing, better go with simple R or R=302

After you've completed your tests, you can use R=301.

remove trailing slash from URL in htaccess

Insert this rule just below RewriteEngine On:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,NE,L]

htaccess add remove trailing slash url to existing rule

You can have your .htaccess as this:

Options -MultiViews
RewriteEngine On

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

# front router rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Htaccess how to remove trailing slash from file but not directory

Insert this rule at top of your .htaccess to remove trailing slash for non-directories:

RewriteEngine On

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

remove trailing slash with htaccess

You can use:

Options +FollowSymLinks
RewriteEngine On
DirectorySlash Off

# remove trailing slash to non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,L]

# add trailing slash internally to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?[^/])$ $1/ [L]

# add .html file ending internally
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]


Related Topics



Leave a reply



Submit