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 simpleR
orR=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
PHP Display Image Blob from MySQL
How to Replace Certain Parts of My String
How to Upload File Using Curl With PHP
Preferred Method to Store PHP Arrays (Json_Encode VS Serialize)
Curl Error 60: Ssl Certificate: Unable to Get Local Issuer Certificate
Highlight the Difference Between Two Strings in PHP
PHP Array Delete by Value (Not Key)
How to Use Password Hashing With Pdo to Make My Code More Secure
Convert Time in Hh:Mm:Ss Format to Seconds Only
How to Check If PHP Array Is Associative or Sequential
How to Compare Two Dates in PHP
MySQL and PHP: Utf-8 With Cyrillic Characters
Property [Title] Does Not Exist on This Collection Instance
PHP Session Fixation/Hijacking
How to Get Id of the Last Updated Row in MySQL
How to Store Repeating Dates Keeping in Mind Daylight Savings Time
Doctrine2: Best Way to Handle Many-to-many With Extra Columns in Reference Table