Forum Moderators: phranque

Message Too Old, No Replies

url rewrite and subdomain rewrite at the same time

combine rules without creating a conflict?

         

buremon

7:33 am on Jan 30, 2010 (gmt 0)

10+ Year Member



i have two rules in my .htaccess file for url rewriting:

1. for subdomain rewriting: #*$!.domain.com is internally redirected to file.php?item=#*$!

RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]

RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]

RewriteRule ^$ /file.php?item=%2 [QSA,nc]

2. ordinary rewriting:

RewriteRule ^([A-Za-z0-9_)(:!-',]+)/?$ file.php?item=$1 [L]

What i need to achieve is writing a third rule that will combine these two rules without being in conflict with them. Namely, below (or above) this lines i need to have something like that:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com/([A-Za-z0-9_)(:!-',]+)$ [NC]

RewriteRule ^$ /anotherfile.php?item1=%2&item2=$1 [QSA,nc]

so that [#*$!.domain.com...] will be redirected to anotherfile.php?item1=#*$!&item2=yyy

any ideas what is the correct way of it?

jdMorgan

3:32 pm on Jan 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The client-requested URL-path will not appear in the value of HTTP_HOST tested by RewriteCond. This variable will contain only the requested hostname, an optional trailing period to indicate an FQDN (Fully-Qualified Domain Name) and an optional HTTP port number. So you must match the requested URL-path in the RewriteRule.

Hyphens within [alternate character groups] and literal periods *not* within [alternate character groups] must be escaped in the regular-expressions patterns as shown.

Try this new rule ahead of your current rules, and fix the problems in your current rules as shown:


RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com [NC]
RewriteRule ^([a-z0-9_)(:!',\-]+)/?$ anotherfile.php?item1=%2&item2=$1 [NC,QSA,L]
#
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com [NC]
RewriteRule ^$ file.php?item=%2 [QSA,L]
#
RewriteRule ^([a-z0-9_)(:!',\-]+)/?$ file.php?item=$1 [NC,QSA,L]

Be aware that by allowing variant-cased domains and URLs to match, and by making the trailing slash optional in rules #1 and #3, you are introducing a potential duplicate-content problem. Your script(s) must validate the requested "item" values, and if any incorrect-case input is detected, it should generate a 301-Moved Permanently redirect to the correctly-cased URL. Allowing the same content to appear at more than one unique URL means that the multiple URLs will compete with each other for links and search engine ranking, and neither will do as well together as one would do alone.

If it is not possible to handle this in your script(s), then the code above should be modified to reject casing errors and trailing-slash errors, and additional rules should be added ahead of them to externally redirect all casing and trailing-slash variants to the correct canonical URL.

All additions, deletions, and modifications in the code above were intentional, and most cause important changes/improvements in the function of the code.

Jim

buremon

12:35 am on Feb 5, 2010 (gmt 0)

10+ Year Member



it works exactly as i want. thanks a lot Jim.