Forum Moderators: phranque

Message Too Old, No Replies

Rewrite for the m parameter

?m=#*$!#*$!xx to index

         

Rhobur

8:44 am on Oct 18, 2011 (gmt 0)

10+ Year Member



Hi,
I want to redirect http://www.example.com/blogg/?m=12345678 where 12345678 is an aleatory 8 digit figure to http://www.example.com/blogg/ and I've come up with this :
RewriteCond %{QUERY_STRING} ^m=[0-9]{8}$ [NC]
RewriteRule (.*) http://www.example.com/blogg/%1/$1? [R=301,L]

still no luck, nothing happens. What am I doing wrong ?
TIA
Robert

lucy24

7:32 am on Oct 23, 2011 (gmt 0)

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



For starters, you're using words like "aleatory" that force people to sidetrack to the dictionary* before they can even start on the question.

Can we assume for the sake of discussion that mod_rewrite is enabled and you've got the

RewriteEngine On

line somewhere up above?

Your Rule as written doesn't match what you say you want to do. It also contains a simple error that you probably noticed about three minutes after the post-editing time limit expired.

RewriteCond %{QUERY_STRING} ^m=[0-9]{8}$ [NC]
RewriteRule (.*) http://www.example.com/blogg/%1/$1? [R=301,L]

Hint: Where is %1 coming from? That's again assuming that you forgot to mention that you wanted it to go there. (I'd have expected $1/%1 but it's your URL.)

Setting aside this typo, the rule says:

take a request in the form
www.example.com/{blahblah}/?m={numbers}

and redirect to
www.example.com/blogg/{samenumbers}/{sameblahblah}

stripping away the former query string. Since the Rule includes stripping the query, you don't need a supplementary Condition to ensure that you're not going into a loop; your existing Condition does the job.


* ###. Can't you just say "randomly generated" like a normal person? Bloody showoff. :-P

g1smd

7:42 am on Oct 23, 2011 (gmt 0)

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



Is the rule only meant for extensionless requests and/or for html pages and not for images and stylesheets? If so, you might want to change the (.*) pattern to be more restrictive in what it accepts.

As an example, manually request an image from your server, but add the m= parameter to your URL request. See what mayhem that request currently causes.

Additionally, when I have a rule which does something with some requests that have parameters, I always have another rule right after that one which simply removes all parameters for all other requests.

lucy24

9:12 am on Oct 23, 2011 (gmt 0)

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



<fe>
Setting aside this typo, the rule says:

take a request in the form
www.example.com/{blahblah}/?m={numbers}
</fe>
I have no idea where that / came from. Cough, cough. Ahem.

And rereading your original post I now realize you don't need the {blahblah} at all, because you say explicitly
http://www.example.com/blogg/?m=12345678

So the Rule just needs to say (with Condition as above)

RewriteRule blogg/$ http://www.example.com/blogg/%1? [R=301,L] 

or
RewriteRule blogg/$ http://www.example.com/%1/blogg? [R=301,L] 


depending on where you wanted the string of numbers to go. No need to capture "blogg/" if it's literal text.

If when you say "blogg/" you really mean "blogg/{moreblahblah}" then you'd capture the extra stuff and plug it in as $1. If so, write the rule as narrowly as possible:

RewriteRule blogg/([^.]+)$ {et cetera}
or
RewriteRule blogg/([^.]+\.html)$ {et cetera}

using the actual extension-- or none-- that your pages have. The idea is to skip the rule entirely if the request is for something in .jpg or .css or similar. Rules are evaluated before Conditions. If some wise guy has typed in

www.example.com/blogg/images/redface.gif?12345678

you do not want the Rule to kick in!

g1smd

6:13 pm on Oct 23, 2011 (gmt 0)

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



Yep, that's the long and short of it.

When designing redirects and rewrites you need to be sure what you want the rule to do and you also need to have an idea what you do NOT want it to do.

Rhobur

10:03 am on Oct 24, 2011 (gmt 0)

10+ Year Member



@lucy24 : thank you for helping; done what you said and it's working. It's just that this rewrite business seem a bit hard to comprehend for me, I probably need a few quiet days for understanding it.
@q1smd : thanks for trying.

lucy24

4:13 pm on Oct 24, 2011 (gmt 0)

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



I probably need a few quiet days for understanding it.

If you can get a solid grip on mod_rewrite with a few days of Deep Thought, this forum needs you :)

Rhobur

6:07 am on Oct 25, 2011 (gmt 0)

10+ Year Member



Thanks again !
Best wishes !