Forum Moderators: phranque
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
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]
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.
#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]
[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.
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 ?
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.
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.
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.
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.
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.
.
Conversion now seemingly continued in another thread: [webmasterworld.com...]