Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite - 2nd rule is not working / conflict

mode rewrite rules conflict

         

fastfriend

8:02 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



I am new in Mod Rewrite.

I have written 2 rules for 2 php files as

RewriteRule ^(.*)/(.*).html /category.php?cid=$1&category=$2 [L]
RewriteRule ^(.*)/(.*).html /item.php?iid=$1&item=$2 [L]

The problem is that when I try to use

www.mydomain.com/11/itemtitle.html (2nd rule)

it find '11' in category.php and run the first rule.

I appreciate your help.

Thanks

g1smd

8:04 pm on Oct 4, 2008 (gmt 0)

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



Sure. The first rule says match "anything" and "everything" and that is what it does. The .* part is "greedy".

You need the first rule to be very specific as to what it will match, otherwise it will not work correctly.

Actually, both rules will need to be very specific, otherwise they will match requests for images, your robots.txt file, and just about anything really.

[edited by: g1smd at 8:08 pm (utc) on Oct. 4, 2008]

fastfriend

8:07 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



How to solve the issue.

Can some write the correct code here.

g1smd

8:10 pm on Oct 4, 2008 (gmt 0)

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



There are a number of similar requests in recent days.

You'll need to study those and understand them, write some code, test it and then post here if you are having trouble debugging stuff.

fastfriend

8:23 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



I have been trying for last 2 days. Many different options I tried but unable to figure out.

After 2 days of tiring efforts I am asking a help.

What I need is

www.***domain.com/catnumber/cat-title-with-dash.html
www.***domain.com/itemnumber/item-title-with-dash.html

If you can correct the code, then thanks.

g1smd

8:25 pm on Oct 4, 2008 (gmt 0)

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



What have you tried?

fastfriend

8:30 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



#http://domain.com/category.php?cid=3&category=text

#RewriteRule ^([^_]*)_([^_]*)_cat\.html$ /category.php?cid=$1&category=$2 [L]

#RewriteRule ^([a-zA-Z0-9\-]+)-([a-zA-Z0-9\-]+)-cat\.html$ /category.php?cid=$1&category=$2 [L]

#RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)$ category.php?cid=$1&category=$2 [L]

#RewriteCond %{HTTP_HOST} ^.*$

#RewriteRule ^/?$ [domain.com...] [R=302,L]

#RewriteRule ^category/(.*)/(.*).html /category.php?cid=$1&category=$2 [L]

#RewriteRule ^category-(.*)-(.*).html /category.php?cid=$1&category=$2 [L]

#RewriteRule ^(.*).html /category.php?cid=$1&category=$2 [L]

g1smd

8:34 pm on Oct 4, 2008 (gmt 0)

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



There's some stuff that might help in:

[webmasterworld.com...]

Again

.*
matches "everything".

You'll need something that matches "a fixed number of digits" or a "fixed number of characters" or "all characters up to the next slash" or "all characters up to the next dot" or something like that in your various rules, and you *must* code it so that all of the redirects are listed before you list any rewrites.

You'll also need to look at that R=302 and likely change it to R=301 instead. In fact, that rule doesn't likely do what you want. It looks like it creates an infinite loop. If it is for redirecting non-www to www then there is a much better way to do it, in the linked thread.

fastfriend

8:52 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



OK. Now I just have try with

RewriteRule ^([^/]+)/([^/]+).html /category.php?cid=$1&category=$2 [L]
RewriteRule ^([^/]+)/([^/]+).html /item.php?iid=$1&item=$2 [L]

The result is same as previous:

www.mydomain.com/11/itemtitle.html (2nd rule)

it find '11' in category.php and run the first rule.

What is going wrong ?

g1smd

8:57 pm on Oct 4, 2008 (gmt 0)

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



Every URL you throw at the rules is matched by the first rule, and the second rule can never get to run.

That's because they both test for <something>/<something>.html - so everything matches the first rule, and when it is done, processing stops.

You need to look at what URLs should match the first rule and what URLs should match the second rule and then work out what is *different* between the two of them; and then make sure that stuff that the second rule should match, cannot be picked up by the first rule.

If cid is always, say, 3 digits, and iid is, say, always 7 digits, the code can be fixed very easily - or, if one is always numbers and the other is always letters, then again it is very easy to test for that.

Only you can know exactly which URL formats should match, and which should be rejected, by any particular rule.

fastfriend

9:24 pm on Oct 4, 2008 (gmt 0)

10+ Year Member



Thanks for the help.

I got it finally working.

What I just did is that I introduced DUMMY characters to make both rules different.

RewriteRule ^C([^/]+)/([^/]+).html /category.php?cid=$1&category=$2 [L]
RewriteRule ^I([^/]+)/([^/]+).html /item.php?iid=$1&item=$2 [L]

previously these were

RewriteRule ^([^/]+)/([^/]+).html /category.php?cid=$1&category=$2 [L]
RewriteRule ^([^/]+)/([^/]+).html /item.php?iid=$1&item=$2 [L]

Thanks for the kind help.

g1smd

8:45 am on Oct 5, 2008 (gmt 0)

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



Yep, that is as good a solution as any in most cases.

Just be sure that there are no URLs that begin with C or I that should not match the rule that are inadvertently being caught by it.

fastfriend

8:22 pm on Oct 6, 2008 (gmt 0)

10+ Year Member



Now, I have a question.

My PHP pages are indexed in Google. How do I tell Google that my page now are HTML. What should I change in my above code to tell the google correctly.

Thanks

g1smd

8:34 pm on Oct 6, 2008 (gmt 0)

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



You need a 301 redirect to force them over to the new URL.

That's some extra lines of code.

Look at this example, and make sure you fully understand it... [webmasterworld.com...]

It shows the process of having both redirects to correct the URL and rewrites to connect the URL request to the internal script - as well as redirects to fix URL canonicalisation.

fastfriend

8:50 pm on Oct 6, 2008 (gmt 0)

10+ Year Member



What I understand is that I should use [R=301,L] instead of [L].

Is it correct?

g1smd

9:00 pm on Oct 6, 2008 (gmt 0)

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



You use [R=301,L] on the redirect part(s).

You use [L] on the rewrite part.

In that example you will see a redirect from one URL type to another URL type, and a rewrite to connect the corrected URL request to the internal filepath where the files actually reside - along with some other fixes.

Compared to the example in the other thread, so far you have done the very last line - the rewrite.

Now you need to do the redirects to stop dynamic URLs being indexed, redirects to fix index file canonicalisation, redirects to fix domain canonicalisation, and maybe the redirects to fix up invalid URL requests.

fastfriend

9:14 pm on Oct 6, 2008 (gmt 0)

10+ Year Member



sorry, i could not understand your point. Can you explain it.

g1smd

10:45 am on Oct 7, 2008 (gmt 0)

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



Which point? There were several.

The example in the other thread should explain itself, each line of code is commented as to what it does.

There are about 20 lines of code, each performing a specific function.

With the code discussed here in this thread, you have replicated the function of just the very last line of the example.

g1smd

9:41 pm on Oct 7, 2008 (gmt 0)

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




This thread has most of the process listed in the main example: [webmasterworld.com...]

.

Conversion now seemingly continued in another thread: [webmasterworld.com...]