Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove trailing slashes from Query String Apache

I am having an issue trying to remove trailing slashes from the end of a query string in apache.

I have the following rewrite rules in place right now to make the URL and Query String all lowercase:

   RewriteEngine On
   RewriteMap lc int:tolower
   RewriteCond %{REQUEST_URI} ^[^A-Z]*[A-Z].* [OR]
   RewriteCond %{QUERY_STRING} ^[^A-Z]*[A-Z].*
   RewriteRule ^ ${lc:%{REQUEST_URI}}?${lc:%{QUERY_STRING}} [L,R=301]

I have tried to add:

RewriteCond %{QUERY_STRING} (.+)/$
RewriteRule ^ %1 [R=301,L]

But it breaks the website. I have been searching for a way to do this but haven't come up with any solutions yet. I tried the answers from this post but they didn't work.

The reason I need to do this is because our application firewall looks for "ID" in the url and if there is any non alphanumeric character that comes after then it blocks the request. The firewall is implemented after the Apache request hits the server.

Hoping someone with more experience with Apache Rewrite rules can help me out. Thanks in advance.

like image 590
Yamaha32088 Avatar asked Oct 24 '25 15:10

Yamaha32088


1 Answers

To remove trailing slash from query string you can use this rule:

RewriteCond %{QUERY_STRING} ^(.+)/$
RewriteRule ^ %{REQUEST_URI}?%1 [R=301,L,NE]

Make sure this is first rule in your .htaccess below RewriteEngine On line.

like image 148
anubhava Avatar answered Oct 27 '25 16:10

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!