Forum Moderators: phranque

Message Too Old, No Replies

Rewrite - Just can't get it right

         

TheSockMonster

8:37 pm on Aug 24, 2011 (gmt 0)

10+ Year Member



Hi

I have spend the last couple of days scouring the internet but I can't figure out how to do this.

Basically I need the page...

www.domain.com/page1/12345678

to redirect to

www.domain.com/page2/page1/12345678

I thought I might be able to do it with mod_alias but I end up in an infinite loop.

What I have so far is

RewriteCond %{QUERY_STRING} ^(.*&)/page1/([0-9]+)?$ [NC]
RewriteRule ^/page1/([0-9]+)?$ /page2/page1/$2 [R=301,L]

but I'm just not hitting the rule.

I have spend hours on this and I just can't work out what I am doing wrong.

Any help would be very much appreciated.

Thanks

Si

g1smd

9:15 pm on Aug 24, 2011 (gmt 0)

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



Why are you looking at the QUERY_STRING server variable?

There's no query string in your example URL.

Add the protocol and domain name to the redirect target URL.

TheSockMonster

9:45 pm on Aug 24, 2011 (gmt 0)

10+ Year Member



Hi

So all i should need is the line

RewriteRule ^/page1/([0-9]+)?$ http://www.example.com/page2/page1/$1 [R=301,L]

I've just tried this and it still isn't hitting the rule.

Thanks

g1smd

10:18 pm on Aug 24, 2011 (gmt 0)

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



Remove the leading slash from the RegEx pattern if the rule is placed in your .htaccess file.

TheSockMonster

10:32 pm on Aug 24, 2011 (gmt 0)

10+ Year Member



Sorry, should have mentioned. This is in the vhosts.conf.

Si

lucy24

12:10 am on Aug 25, 2011 (gmt 0)

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



Psst! The easiest way to avoid unwanted smileys is to click "Disable graphic smile faces":

RewriteCond %{QUERY_STRING} ^(.*;)/page1/([0-9]+)?$ [NC]
RewriteRule ^/page1/([0-9]+)?$ /page2/page1/$2 [R=301,L]


The Cond as written is gibberish. But the Rule would not have worked in any case, because captures from the Conditions are expressed as % while $ is reserved for captures from the Rule. And each one is counted separately from 1 on.

The Query String %{QUERY_STRING} is the part of the url after the question mark. You want something with REQUEST in it.

It is always a good idea to start by saying in English, preferably in words of two syllables or less, just what you want your rewrite or redirect to do. Here it's the common pattern "add blahblah to the request". But all changes in this form have an implied Condition "if it isn't already present". Leave this out and you get your infinite loop. That's why you have to go to mod_rewrite. Redirects via mod_alias don't "do" conditions.

The flag [L] doesn't mean "get out of this .htaccess (or config or whatever) and never set foot in it again". It just means "stop here and start the whole htaccess business from the top, repeating until you get all the way to the end".

So you need a

RewriteCond %{REQUEST_FILENAME} !page2

g1smd

12:28 am on Aug 25, 2011 (gmt 0)

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



Actually,
^/page1/([0-9]+)?$
allows only two folder levels, the first of which must start with "page1" so the new URL with three folder levels (and beginning "page2") will not cause an infinite loop.

Is the rule located in the right "container" in the vhosts file?

lucy24

3:47 am on Aug 25, 2011 (gmt 0)

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



OK, I'm overlooking something obvious again. What's the difference between

/([0-9]+)?$
and
/([0-9]*)$

?

g1smd

8:19 am on Aug 25, 2011 (gmt 0)

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



Coding style and a few nano-seconds. Both have same end result.