Jivani Jaydeep Jivani Jaydeep - 4 months ago 36
PHP Question

Removing the last slash from query parameter using .htaccess

I want to remove the last slash character from query parameter's value using


Suppose I have the following URL:


Then from
I want to remove the last slash.

After removing the last slash, it would redirect to
but with my current settings it redirects to

My current
file looks like this:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^key_=(.*)$
RewriteRule ^(.*)$ snapshots/%1.html [R=301,QSD]


You don't need to capture anything in RewriteRule, as all necessary information is already captured by RewriteCond. To remove the trailing slash use an expression like \/*$ (zero or more slashes at the end):

RewriteEngine on
RewriteCond %{QUERY_STRING} ^key_=(.*?)\/*$
RewriteRule ^.*$ /snapshot%1.html [R=301,QSD]