Forum Moderators: phranque

Message Too Old, No Replies

subdomain to page mod rewrite question

mod rewrite a subdomain to page

         

Toucan42

6:00 pm on Sep 14, 2007 (gmt 0)

10+ Year Member



Hello everyone.

I am a complete newbie to regular expressions and mod_rewrite and am faced with a specific need that I cannot figure out.

When a user visits:

[USERNAME.example.com...]

I need it instead to go to:

http://www.example.com/page.php?username=USERNAME

I cannot figure out how to get this right in htaccess using mod_rewrite

Any help would be GREATLY appreciated - thank you!

[edited by: jdMorgan at 11:20 pm (utc) on Sep. 14, 2007]
[edit reason] example.com [/edit]

jdMorgan

11:21 pm on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you looked at RewriteCond, using the %{HTTP_HOST} variable? Or do you have a problem with regular-expressions?

It would be most helpful if you would post the code you've tried, as a basis for discussion.

Thanks,
Jim

Toucan42

3:24 am on Sep 15, 2007 (gmt 0)

10+ Year Member



Hi -

apologies for not posting my incorrect code.

I know little to nothing about regular expressions, but here's the code (not working) I have currently:

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}!www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule (.*) %2/page.php?username=$1 [L]

so...

the idea here is that someone coming to [USERNAME.example.com...] will land instead at http://www.example.com/page.php?username=USERNAME

Hope this helps

jdMorgan

2:04 pm on Sep 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A few minor problems with unescaped literals an anchoring, but the major problem appears to be an unchecked 'infinite' rewrite loop; Here we add a RewriteCond to prevent recursion on the rewrite to <anything>/page\.php:

Options +FollowSymLinks +Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com [NC]
RewriteCond $1 !page\.php$
RewriteRule (.*) /%2/page.php?username=$1 [L]

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim