Forum Moderators: phranque

Message Too Old, No Replies

i cant get it ! rewriting to a virtual subdomain with wildcard

         

Camaleon

4:52 am on Nov 20, 2008 (gmt 0)

10+ Year Member



Hi, iam using wildcard and it is working fine because any something.mydomain.com redirects to mydomain.com but i need to rewrite this:

www.mydomain.com/index.php?id_pro=111 to www.111.mydomain.com

or

www.mydomain.com/index.php?id_pro=something to www.something.mydomain.com

I think that both are the same, i tried several rules but i cant get it !

this was the last rule i try:

rewriteEngine on
rewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com [NC]
rewriteCond %{HTTP_HOST) !^www\.
rewriteRule .* index.php?id_pro=%1 [L]

Thanks alot.

g1smd

5:13 pm on Nov 30, 2008 (gmt 0)

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



The leading
(.*)
should be changed to something else as it is very inefficient. Perhaps
([^/]+/)
or something.

Camaleon

5:53 pm on Nov 30, 2008 (gmt 0)

10+ Year Member



Ok, i remove it for testing now, and it is going to the file, the only problem now is that i obtain a mysql error, because the sql query dosent find the id_c value (8 in my example)

Any idea why ?

Camaleon

12:28 am on Dec 1, 2008 (gmt 0)

10+ Year Member



ok g1smd, i will try, i didnt see your post !

Camaleon

12:57 am on Dec 1, 2008 (gmt 0)

10+ Year Member



Well folks ! i did it ! it is working !

RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/(index\.php¦my_script\.php¦robots\.txt¦sitemap\.xml¦labels\.rdf¦w3c/p3p\.xml)$
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦jpeg?¦png¦ico¦css¦js¦avi¦mpe?g¦wav¦wmv¦mp3¦swf¦flv)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule ([0-9]*)([0-9]*).html$ my_script.php?id_c=$1&id_pro=%2 [L]

I used ([0-9]*) to test whit numbers because whit the other way didnt work, but at least i know that the rule is fine, i will change that part in the near future.

Thanks a lot morgan, g1smd and phranque, i thought that this was going to be more difficult, i knew very little about mod rewrite.

g1smd

9:49 am on Dec 1, 2008 (gmt 0)

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



([0-9]*)
says "zero digits or more than zero digits", so repeating it makes no sense.

If you need to check for a certain number of digits use:

([0-9]{3})
- exactly three digits, or
([0-9]{2,5})
- between two and five digits.

jdMorgan

10:51 pm on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule ^([^.]+)\.html$ my_script.php?id_c=$1&id_pro=%2 [L]

I deleted the first RewriteCond line, because none of the URL-paths in that line ended with ".html" and I deleted the second RewriteCond because none of the listed filetypes were ".html". Since the RewriteRule requires the URL-path to end in ".html" there was no reason to test for any specific non-html files.

I also got rid of the "(.*)(.*)" pattern, because it was ambiguous and because the second "(.*)" pattern would always result in an empty match. This is because the first "(.*)", being a "greedy" pattern, would have already matched as many characters as possible.

Jim

This 36 message thread spans 2 pages: 36