Forum Moderators: phranque

Message Too Old, No Replies

strip & from url

htaccess code for striping & and anything after

         

santapaws

1:19 pm on Sep 28, 2011 (gmt 0)

10+ Year Member



Just wondering how to remove urls that are requested with things like
&wotever after the html. For example a real url is example.html but its linked by example.html&wotever or example.html&random. I would like to remove the & and everything after it.

a second issue is i have
RewriteCond %{query_string} .
rewriterule (.*) http://www.example.com/$1? [R=301,L]

It works on all urls except on the home url of domain.com/? and not sure why.

Tx.

[edited by: santapaws at 1:38 pm (utc) on Sep 28, 2011]

g1smd

1:21 pm on Sep 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll need to define what the stuff you want to remove "looks like" so you can design a RegEx pattern that matches it and does not match stuff you do not want to remove.

In theory it could be as simple as

RewriteRule ^([^&]*)& http://www.example.com/$1? [R=301,L]


Fix the casing of RewriteRule and of QUERY_STRING in your original code.

Use example.com in this forum to prevent URL auto-linking.

santapaws

4:04 pm on Sep 28, 2011 (gmt 0)

10+ Year Member



well there are no urls that have anything after the html. Im getting a lot of spurious searches for urls with added & plus a random string and id like to redirect to the actual url. So it could anything like &3234345jfefdgfdgrf

lucy24

6:51 pm on Sep 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



well there are no urls that have anything after the html.

Yes, there are. You started out by asking how to get rid of them. What you mean is that your site doesn't use urls in this form-- as indeed it shouldn't, since & is supposed to connect multiple parts of a query string.

Did you try the RewriteRule suggested above? It should get rid of everything from the first & on to the end. That's assuming the extra stuff really does start with &, not the expected ? (literal question mark, meaning query string).

And then see if you can find where those spurious urls are coming from. It's unusual for outside sites to attach stuff out of thin air-- especially malformed stuff.

For the second question: Try to school yourself not to say "it doesn't work". You know what it means, but the reader doesn't. Do you mean that nothing happens, or that something unintended happens?

santapaws

7:23 pm on Sep 28, 2011 (gmt 0)

10+ Year Member



RewriteRule ^([^&]*)& http://www.example.com/$1? [R=301,L]
doesn't work for anything unfortunately but suggestion was appreciated