Forum Moderators: phranque
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!
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
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.
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]
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