Forum Moderators: phranque

Message Too Old, No Replies

.htaccess subdomain to script query rewrite

         

IrTakWeb

2:51 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



Hi
i Have a virtual subdomin system in my website and that do a simple job. like this :
when you enter a url like subdomain.mydomain.it
its redirect to :
mydomain.ir/product.php?product_id=subdomain
and that was work well before .
i use this .htaccess code :

RewriteEngine On

# Extract the subdomain part of domain.com
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.ioo\.ir$ [or]
RewriteCond %{HTTP_HOST} ^([^.]+)\.ioo\.ir$ [or]

# Check that the subdomain part is not www and ftp and mail
RewriteCond %1 !^(www¦ftp¦mail)¦()$ [NC]

# Redirect all requests to a php script passing as argument the subdomain
RewriteRule ^.*$ product.php?product_id=%1

but now i change my webhost and it dosent work !
i try a dozen type of .htaccess code for this.
and i check and see that the mod_rewrite work well ,

this problem take 2 days of me , please help me .
<snip>
thanks for your help

[edited by: jdMorgan at 4:18 pm (utc) on Mar. 25, 2009]
[edit reason] Do not post personal info -- Thanks! [/edit]

g1smd

4:40 pm on Mar 25, 2009 (gmt 0)

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



All the things you call redirects above are rewrites, not redirects.

Which action do you really want?

Don't forget to add [R] to the end of any Rules.

Is Mod_Rewrite actually enabled on this server?

jdMorgan

5:04 pm on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That code should not work on any server, since the %1 variable will be cleared by the final, negative-match RewriteCond. You also had an extra --and incorrect-- [OR] flag on your second RewriteCond. Try this instead:

# Check that the (sub)-subdomain is not www and ftp and mail
RewriteCond %{HTTP_HOST} !^(www\.)?(www¦ftp¦mail)\.example\.com [NC]
#
# Extract the (sub)-subdomain part of sub.example.com (with optional leading "www.")
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example.\.com
#
# Internally rewrite all requests to script, passing the subdomain in query string as "product_id"
RewriteRule ^.*$ product.php?product_id=%2 [L]

If this does not work, please be more specific about what is wrong: How did you test (what URLs)? What were the results? How did those results differ from the results you expected?

Note that it is not necessary to check explicitly for a blank sub-subdomain; Any request with a blank sub-subdomain will not match the second RewriteCond.

Important: Replace the broken pipe "¦" characters with a solid pipe before use; Posting on this forum modifies the pipe characters.

I should also note that www.xyz.example.com is a sub-sub-domain, and there is little reason to require your users to enter long, ugly domain names that way: "xyz.example.com" for products and "www.example.com" for the main site would make a lot more sense. See for example "search.yahoo.com"

Remember to completely flush your browser cache before testing any new server-side code.

Jim

[edited by: jdMorgan at 5:09 pm (utc) on Mar. 25, 2009]

IrTakWeb

8:09 pm on Mar 25, 2009 (gmt 0)

10+ Year Member



g1smd
jdMorgan

thanks for your replays but my problem is still alive!

Dear g1smd
I want to rewrite.
and i test other simple rewrite code that sho the mod_rewrite is enable . (i test this for checking that :
RewriteRule (.*) [yahoo.com...]
and this work well
and i find out that my mod_rewrite module is enable)

Dear jdMorgan
i test your code and unfortunately it does not work
i check every thing that i can but i'm not an expert in this field

i put my host username and password here ,
if anyone can go and correct my mistakes , (Please Help)

<snip>

thank a lot

[edited by: jdMorgan at 12:32 am (utc) on Mar. 26, 2009]
[edit reason] Do not post personal information. [/edit]