Forum Moderators: phranque

Message Too Old, No Replies

Simple subdomain url rewriting

         

DeezerD

10:44 am on Sep 26, 2007 (gmt 0)

10+ Year Member



Hi there
here is my problem:
I would like to set up a very simple rewriting subdomain rule that should look like this:
http://subdomain.example.com => http://www.example.com/index.php?var=subdomain

so I tried this rewrite rule but it doesn't work and I don't see what is wrong in it:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule (.*) http://www.example.com?index.phpvar=$1 [R=301,L]

thanks in advance for your help

[edited by: jdMorgan at 2:02 pm (utc) on Sep. 26, 2007]
[edit reason] example.com [/edit]

DeezerD

10:47 am on Sep 26, 2007 (gmt 0)

10+ Year Member



"little" mistake in the rule I wrote, I actually use this one (that still doesn't work anyway)

RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule (.*) http://www.example.com/index.php?var=$1 [R=301,L]

thanks

[edited by: jdMorgan at 2:03 pm (utc) on Sep. 26, 2007]
[edit reason] example.com [/edit]

jdMorgan

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

WebmasterWorld Senior Member 10+ Year Member



You need to provide the subdomain to the substitution URL using a back-reference to the RewriteCond. Something like:

RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule .* /index.php?var[b]=%1[/b] [L]

The first (new) RewriteCond prevents an 'infinite' rewriting loop.

The second (new) RewriteCond prevents rewriting the "www" subdomain. Delete it if you do want "www" rewritten to the script.

Usually, it is not desirable to use a redirect, because that exposes the underlying subdomain->script mechanism. Here, I show an internal rewrite only, which does not expose the script to clients.

Lastly, what do you intend to do if someone requests subdomain.example.com/some-page-here.php ? The current code will simply discard the URL-path for the requested page.

Jim

[edited by: jdMorgan at 2:06 pm (utc) on Sep. 26, 2007]

DeezerD

2:42 pm on Sep 26, 2007 (gmt 0)

10+ Year Member



first, thanks for your reply

RewriteCond %{REQUEST_URI}!^/index\.php$
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule .* /index.php?var=%1 [L]

I just tried your code and I can't make it work
I replaced "example" by my domain name and I get the "Server not found" error message

Lastly, what do you intend to do if someone requests subdomain.example.com/some-page-here.php?

I'm working on a blog platform actually: each subdomain name refers to the blog user name
For example:
[foo.example.com...] must refers to http://www.example.com/index.php?blog=foo
If I follow the rewrite rules you posted, and besides any php security holes, is there any other rewrite rules I should set up to prevent security issues?

many thanks for your help

jdMorgan

3:17 pm on Sep 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you typing in a subdomain when you get this "Server not found" message?

Have you set up wild-card DNS for your subdomains?
Does your host "map" all subdomains to your main 'account' directory?

In addition to adding the rewriterule, these other two steps are also required if not already done.

Jim

DeezerD

3:42 pm on Sep 26, 2007 (gmt 0)

10+ Year Member



Have you set up wild-card DNS for your subdomains?

I just asked to my hosting support to do so and it does work now, thanks a lot :)

Just another question for the whole understanding of the process, could you indicate what is the line for setting up wild card DNS?

Is it:
ServerAlias *.example.com

once again, thanks for your great help

jdMorgan

3:50 pm on Sep 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, you have to edit your DNS zone file, using whatever 'tool' is provided by your domain registrar, and add an A Record (preferred) or C Name for "*.example.com." to map it to your server's IP address. (Step 1 above)

The ServerAlias directive you cite is used on the server itself to map the subdomains into your "account space." (Step 2 above)

Jim