Jivani Jaydeep Jivani Jaydeep - 28 days ago 9
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

.htaccess
.

Suppose I have the following URL:

www.example.com/?key_=/job_category/mobile/


Then from
/job_category/mobile/
I want to remove the last slash.

After removing the last slash, it would redirect to
www.example.com/snapshots/job_category/mobile.html,
but with my current settings it redirects to
www.example.com/snapshots/job_category/mobile/.html
.

My current
.htaccess
file looks like this:

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

Answer

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]