1

How to force remove trailing slash from specific URL using htaccess,

For example :

https://blahblah.com/checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1/

to

https://blahblah.com/checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1

and ignore other URL such as https://blahblah.com/about/ or https://blahblah.com/contact/ etc

2
  • Could you show us what the specific URL is? Commented May 30, 2014 at 9:23
  • 2
    Technically it's part of the QueryString, i.e. options[price_id]=1/ is assigning that GET var to being 1/ rather than just 1 ... I've not fully thought it through, admittedly, but I think you're going to need a RewriteCond to match on the querystring with %{QUERY_STRING} - or you could just cast that GET var to being an (int) in the PHP code. Commented May 30, 2014 at 9:32

4 Answers 4

1

Because the trailing slash is part of the querystring you can't just rewrite it, you have to extract the querystring minus that final / and then redirect to the page you're on with that match appended.

To do this you need to match on the pattern in the RewriteCond with %1 (see this answer for reference) and append that to the %{REQUEST_URI} (thus removing the original querystring) - like this:

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

RewriteCond %{QUERY_STRING} ^(.*)/$ <-- make sure you've got that / at the end here in your conditional - it ensures that only querystrings that end with / are redirected so you don't end up in a horrible recursive loop.

Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

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

This will redirect every request containing query strings only. In your case this:

checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1/

to

checkout/?checkout=add_to_cart&id=17&item_options[price_id]=1

But not this https://blahblah.com/about/ and https://blahblah.com/contact/

5 Comments

Are you sure this wouldn't trigger an infinite loop? It would match http://test.com/ too?
@Diamondo25 No it won't.
interesting that this is the accepted answer as it won't actually work... RewriteRule only matches the path, not the querystring - wasn't me that down-voted it though.
@CD001 Right. I thought that query strings can be match in RewriteRule (Lack of mod_rewrite docs). Ok updated the answer.
Not tested it but it looks like it should work now - have +1 to counter that downvote ;)
0

I would advise trying to find out why that trailing slash is there to begin with first. As CD001 said in the comments it could be that your PHP code is doing something wrong.

If however you do need a htaccess solution to remove the slash then the below should work for you.

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

This has been tested at http://htaccess.madewithlove.be/ and works for your example URLs.

Comments

0

if you mean an internal routine

RewriteCond %{QUERY_STRING} ^(checkout\=[^\&\s]+\&id\=\d+\&item_options\[price_id\]\=\d+)\/
RewriteRule ^ %{REQUEST_FILENAME}?%1 [L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.