Forum Moderators: phranque
Then you'll need to set up your server configuration to accept wildcard subdomains (e.g. using the ServerAlias directive' and to point them to the filespace of the main domain. Then within that filespace or within the VirtualHost configuration section for these servers, you will put the rewrite code to extract the HTTP_HOST and rewrite that information to the query string for your script(s).
When ready to deploy, you will need to delete the records in the hosts files, and define a wildcard A record or CNAME in your 'real' DNS zone file.
Jim
So in your method you descibed above how do I setup the server alias? Also are you suggesting using something other then mod rewrite?
username.localhost/mypage
turn into this:
localhost/page.php?page=mypage&user=username
write now I have this in mod_rewrite(I realize this would only handle the subdomain portion at the moment)
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}!www\.localhost [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.localhost$ [NC]
RewriteRule ^page.php?user=%1 [L]
But all this does is takes me to my main index page and it doesn't seem like the variables get passed on. Any ideas?
Jim
[edited by: jdMorgan at 12:33 am (utc) on Dec. 27, 2007]
I've made a change in my host file to point test.localhost to the folder I want it to go to. I've tried several different rewrite samples that I found but none of them seem to work. I either just get the homepage of my site or a file not found error. I know this is pretty simple for someone that knows what they are doing I just don't understand all the regular expression syntax that is used.
Thanks
The members are here to help you learn, not write your code for you -- as outlined in our forum charter [webmasterworld.com]. Basically, we can help, but it is up to you to learn all about regular expressions pattern-matching and the mod_rewrite directives if you want to be successful in reaching your goal without damaging your site's search rankings or functionality. Without a full understanding of the code and how it applies to your site specifically, you risk copying code off 'some site on the Web' that may do either -- or both.
Thanks,
Jim
[added] It is also presumed that you have searched WebmasterWorld using the keywords in your thread title (and related terms) to find the dozens of previous threads [google.com] on this subject. [/added]
[edited by: jdMorgan at 1:01 am (utc) on Jan. 3, 2008]
RewriteCond %{HTTP_HOST}!^www\.domain\.net
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.net
RewriteRule ([^/]+)\.html?$ /test.php?user=%1&page=$1 [L]
Now my question is even though this works is there something wrong with it or some best practices I need to add? You briefly mentioned what sounded like some pitfalls that I'd like to avoid if at all possible.
Thanks
RewriteRule ([^/]+)\.html?$ /test.php?user=%1&page=$1 [L]
So, it's a better practice to redirect all pages from one form to the other before doing this internal rewrite, and to only link to one form or the other. You can do this 'all at once' with a single directive if you have been consistent with your site design and have only used one of these extensions in your internal linking. If, however, you have used a mixture of the two with no over-arching 'rule' about how they were used (for example, per-directory .htm vs. html), then you may need a list of individual redirects to fix this problem.
In addition, because the rule pattern is not start-anchored, it will accept the last path part of any URL and pass that to your script, ignoring any subdirectory paths prepended to it. Again, this creates duplicate content issues.
Also, be sure that your 'test.php' script will return a 404-Not Found or 410-Gone response for any page which does not exist within a particular user's space, and especially if the requested user-subdomain does not exist.
The basic rule is this: A given 'page' --as defined by its unique content-- must be returned for one URL and one URL only on your site. Any exceptions risk the so-called "duplicate-content penalty".
You may also wish to externally redirect any direct client requests for test.php back to the 'static' subdomain-and-page URL in case a search engine sniffs out a link to your script URL somewhere, or in case someone maliciously exposes that URL to the search engines. This can be done using the same basic technique discussed here as 'redirect index.html to /'.
Ignoring the html/html problem for the moment, I'd suggest fixing the path-part problem and one efficiency tweak to your rule, as long as the resulting rule still meets your requirements:
RewriteRule ^([^/.]+)\.html?$ /test.php?user=%1&page=$1 [L]
Jim
Thanks again for such a wonderful help.
Can you post the .htaccess file content? I am getting Internal Server Error. Below is the content of my .htaccess.
RewriteCond %{HTTP_HOST}!^localhost
RewriteCond %{HTTP_HOST} ^([^.]+)\.localhost
RewriteRule ([^/]+)\.html?$ /test.php?user=%1&page=$1 [L]
I have setup host so that i can access subdomain
127.0.0.1 localhost
127.0.0.1 test1.localhost
127.0.0.1 test2.localhost
Best regards,
K.
The pattern in the rewriterule should be start-anchored for efficiency.
If you have no other working rewriterules, then the second line below is required, and the first line may also be needed. However, the first line may not be allowed if it's not needed, and the only way to find out is to test.
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^([^.]+)\.localhost
RewriteRule ^([^/]+)\.html?$ /test.php?user=%1&page=$1 [L]
When you get a 500-Server Error, check your server error log file; It will often tell you exactly what is wrong.
Flush your browser cache completely before testing any new code.
Jim
I want rewrite rule such that it use the same url instead of test.php but it should pass the user and page. That mean if i open test.localhost/sample.php?id=1 than rewrite rule should work like sample.php?user=test&page=sample
I hope it will make sense...
RewriteRule ([^/]+)\.html?$ /test.php?user=%1&page=$1 [L]
Also one question asked in previous post which is given below:
Just want to confirm the setup of wildcard subdomains is possible on live server.
Best regards,
K.
Setting up wild-card subdomains may require modifications to your DNS zone file, and your host must also support wild-card subdomains and allow requests for all subdomains to be "sent" to the directory where your mod_rewrite code resides. Generally, it's easy to set up the DNS A record to point all subdomains to your server's IP address (or use a CNAME if necessary). However, only testing or asking your host can determine if the host allows wild-card subdomains and will support them as described.
Some hosts won't allow it or will want to charge extra. Some will tell you that they only support functions that are available in your control panel. On others, it's not a problem at all.
Jim