Forum Moderators: phranque

Message Too Old, No Replies

newbie.. mod_rewriting subdomains

         

dend

4:10 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



hey kind people,

I need to do something like this:
when user access [john.mydomain.com...] (or [john.mydomain.com)...]
index.php which is on simple public_html folder should get rewrited like [mydomain.com...]

Is it possible to do that? Can someone give .htaccess code ?

Thank you very much!

jdMorgan

8:37 pm on Oct 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dend,

Welcome to WebmasterWorld!

It's possible to do this, easy in fact. But we don't provide a code-writing service here, because we'd be overrun with requests if we did. Instead, we discuss techniques and problems with your code.

Take a look at the resources cited in our charter [webmasterworld.com] -- The URL Rewriting guide in particular will give examples you may be able to put together to solve this problem.

You may also want to try a search [google.com] for the many previous discussions of this subject.

If you have trouble, post your code and test results, and we'll try to help you get it working.

Jim

dend

11:40 pm on Oct 15, 2004 (gmt 0)

10+ Year Member



Thank you for hint!

I did some search here about this rule:

RewriteCond %{HTTP_HOST} ^.*mydomain\.com$
RewriteRule ^(.*)$ show.php?what=$1

It is working fine, but we also use this rewrite:
www.mydomain.com/article1.html rewrites to www.mydomain.com/index.php?page=article1 and here is rule:
RewriteRule ^(.*).html$ index.php?page=$1

So once our file access file now is:
---------
RewriteCond %{HTTP_HOST} ^.*mydomain\.com$
RewriteRule ^(.*)$ index.php?user=$1
RewriteRule ^(.*).html$ index.php?page=$1
---------
But it is not working good. page article1.html is not loading, also if you type john.mydomain.com images are not loading.

What is wrong here with this code?
Thank you for you help.

jdMorgan

12:57 am on Oct 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To clarify, are you writing code to be placed in .htaccess or code to be placed in httpd.conf?

Jim

dend

2:09 pm on Oct 16, 2004 (gmt 0)

10+ Year Member



jdMorgan thank you that you reply to me

code is placed to .htaccess

But those 2 rules are not working together.

jdMorgan

3:07 pm on Oct 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I asked about httpd.conf and .htaccess because the mod_rewrite syntax differs slightly between the two environments.

The main problem with your combined code is that ".*" matches anything, so the first rule will always be invoked, and the second rule will never be invoked. Requests for images, CSS files, etc. will also be rewritten to index.php. But there is more to it than that...

Do you have a user named "www"? Probably not, so what should happen if someone accesses your site using www.mydomain.com? Make sure you do not allow a user to create a user-subdomain called "www" -- It could disable a large part of your site.

What if someone requests www.subdomain.mydomain.com or subdomain.www.mydomain.com?

You will need to come up with a complete plan that includes all possibilities.

The following demonstrates the use of RewriteConds and specific patterns (instead of ".*") to do what I think you said you want to do:


# If no-www domain requested, externally redirect to www domain
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule (.*) http://www.mydomain.com/$1 [R=301,L]
#
# If www+subdomain domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mydomain\.com
RewriteRule (.*) http://%1.mydomain.com/$1 [R=301,L]
#
# If subdomain+www domain requested, externally redirect to subdomain without "www"
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.mydomain\.com
RewriteRule (.*) http://%1.mydomain.com/$1 [R=301,L]
#
# If www domain requested, rewrite html page requests to show.php with query string
RewriteCond %{REQUEST_URI} !^/show\.php
RewriteCond %{HTTP_HOST} ^www\.mydomain.com
RewriteRule ^([^.]+)\.html /show.php?page=$1 [L]
#
# If subdomain requested, rewrite subdomain and html page requests to index.php with query string
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule ^([^.]+)\.html /index.php?user=%1&page=$1 [L]
#
# If www domain requested, rewrite home page requests to show.php with query string page = "home"
RewriteCond %{REQUEST_URI} !^/show\.php
RewriteCond %{HTTP_HOST} ^www\.mydomain.com
RewriteRule ^$ /show.php?page=home [L]
#
# If subdomain requested, rewrite home page requests to index.php with query string user=subdomain & page="home"
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteRule ^$ /index.php?user=%1&page=home [L]

Any requests for files that do not end in ".html" will not be rewritten to the scripts, except for home page requests in the form "www.mydomain.com/" or "subdomain.mydomain.com/" where the "filename" is blank.

You showed two php files, "show.php" and "index/php" -- I have kept them separate in the code above, but you should be able to correct the code if you really use only "index.php".

This may or may not cover all possibilities on your site -- but you will have to. Study the resources cited in our charter [webmasterworld.com] until you understand how this code works, and then modify it to suit your needs. This code is untested.

Jim

dend

8:23 pm on Oct 16, 2004 (gmt 0)

10+ Year Member



jd - what you did is perfect, the highest respect to you!

keep the Apache Web Server forums running.

OK I am going to study mod_rewrite codding now, this is really a useful thing.

Thanks again.