Forum Moderators: phranque

Message Too Old, No Replies

Conficting rules

Two rules cause a strange URL

         

century2k

9:20 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



Hi

I have a situation where my rules are confilcting creating rather strange results.

I have:

redirect 301 /broadband/AOL+broadband/AOL+Gold http://www.example.co.uk/broadband/AOL+broadband/6

to redirect any old pages to new permanent links

and then:

RewriteRule ^broadband/(.[^/]*)/{0,1}$ /providers.html?name=$1

which is the nice looking URL rule

However if I enter the URL

http://www.example.co.uk/broadband/AOL+broadband/AOL+Gold

I get

http://www.example.co.uk/broadband/AOL+broadband/6?id=AOL+Gold

in the browser

when I want

http://www.example.co.uk/broadband/AOL+broadband/6

If you could help it would stop me going insane

Thanks

Andy

[edited by: jdMorgan at 12:05 am (utc) on Feb. 17, 2006]
[edit reason] Example.com. Please see TOS & charter. [/edit]

jdMorgan

12:03 am on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Andy,

Welcome to WebmasterWorld!

Don't mix mod_alias and mod_rewrite redirects -- You will have no control over their order of execution. Also, both directives will be applied if the URL-prefix or URL-pattern matches.

I'd suggest you re-code your Redirect directive as a RewriteRule, and use the [L] flag on it.

Jim

century2k

6:08 am on Feb 17, 2006 (gmt 0)

10+ Year Member



Thanks for you advice and the welcome. I will not mix the two togther.

However I have tried the following and it still does not work:

RewriteRule ^broadband/AOL+broadband/AOL+Gold$ http://www.example.co.uk/broadband/AOL+broadband/6 [R=301]

RewriteRule ^broadband/(.[^/]*)/(.[^/]*)/{0,1}$ /products.html?id=$2

What I would expect is the first rule to drive the input to the second rule. I couldn't include your [L] flag because I need it to continue to the next rule.

What I actually get is a 404. My rewrite_log is empty so no clues there.

Andy

[edited by: jdMorgan at 6:48 am (utc) on Feb. 17, 2006]
[edit reason] Examplified. [/edit]

jdMorgan

6:42 am on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it continues to the next rule, then your internal rewrite will get 'exposed' by the pending 301 redirect.

You want the old->new URL external redirect to take place first. Then the browser asks for the new URL in a new HTTP transaction, which the second rule will then catch and rewrite silently to your products.html page.

Use the [L] flag unless you have proven cause not to. On both rules. Also, you need to escape any characters in your patterns that are used as regex tokens, such as "+" meaning "one or more of the preceding character, character-set, or grouped subpattern."


# External redirect
RewriteRule ^broadband/AOL\+broadband/AOL\+Gold$ http://www.example.co.uk/broadband/AOL+broadband/6 [R=301,L]
# Internal rewrite
RewriteRule ^broadband/([^/]+)/([^/]+)/?$ /products.html?id=$2 [L]

Jim

century2k

7:44 am on Feb 17, 2006 (gmt 0)

10+ Year Member



Jim

You are a Top Bloke.

Thank you - it did the trick. I think I need more swatting up on the whole area of mod-rewrites. Is the best resource the Apache online manual or would you recommend another resource?

Once again thank you.

Andy

jdMorgan

7:46 am on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For more information on mod_rewrite and regular expressions, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim