rgdigi
rgdigi

Reputation: 1747

Remove trailing slash and create query string with .htaccess

In my .htaccess file, I would like to remove the trailing slash from the URL without modifying the current setup for the query string.

I tried this in a two-step fashion, but it's not working as I expect:

# Remove trailing slash
RewriteRule ^(.+)/$ $1 [R=301]
# Create query string from canonical URL
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 

Cheers

Upvotes: 1

Views: 955

Answers (1)

anubhava
anubhava

Reputation: 785521

You can use:

RewriteEngine On
RewriteBase /site/dev/

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 

Upvotes: 1

Related Questions