Forum Moderators: phranque

Message Too Old, No Replies

wildcard subdomains using wamp

         

test1

8:09 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



I'm not a new web developer but I don't know a lot about the server side of things. I have a local testing environment setup right now using wamp. I'm trying to use mod rewrite to dynamically create subdomains for users but I'm running into a snag. I have know idea how or even if you can add the wildcard domain functionality to localhost with what I'm using. I have wamp installed and i'm running windows vista. Any help would be really appreciate. Thanks.

coopster

2:03 am on Dec 21, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, test1.

I'm trying to use mod rewrite to dynamically create subdomains

mod_rewrite won't create the subdomains for you. Can you offer a little more insight in plain terms what you are attempting to accomplish?

test1

4:24 am on Dec 21, 2007 (gmt 0)

10+ Year Member



Sorry I wasn't descriptive enough. I'm trying to take something like username.domain.com and turn it into domain.com?user=username. I've found posts on how to do this but I don't know if it can be accomplished on my local setup since I don't have a dns server or anything like that. From what I read I need to setup wildcard dns so that it ignores the subdomain and just uses it to pass the variables. Does that help?

jdMorgan

2:25 pm on Dec 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can modify the 'hosts' file (no extension, the filename is just 'hosts') on each machine on your network to define the subdomains as pointing to a server within that network. However, the 'hosts' file does not support wildcards, so you will have to explicitly define each subdomain.

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

test1

3:45 pm on Dec 21, 2007 (gmt 0)

10+ Year Member



Thanks, I was worried I'd have to use the hosts file because it doesn't accept wildcards. It should work for testing purposes I guess I'll just have to create a subdomain for every account I want to test.

So in your method you descibed above how do I setup the server alias? Also are you suggesting using something other then mod rewrite?

test1

9:01 pm on Dec 26, 2007 (gmt 0)

10+ Year Member



Ok, I have it setup now to where I can type username.localhost and get my main site just fine but I can't seem to get mod_rewrite to work with it. Ultimately my end goal is to have this:

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?

jdMorgan

12:00 am on Dec 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your RewriteRule is malformed (bad syntax) and is missing one of the parameters that you specify in your example URL. I'd recommend starting here [httpd.apache.org] with the syntax description.

Jim

[edited by: jdMorgan at 12:33 am (utc) on Dec. 27, 2007]

test1

12:08 am on Jan 3, 2008 (gmt 0)

10+ Year Member



Ok if someone could give me some more specific help with this I would really appreciate it. I showed above how I'm trying to convert my url.

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

jdMorgan

12:57 am on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your best-effort code as a basis for discussion, and ask very specific questions.

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]

test1

7:38 am on Jan 3, 2008 (gmt 0)

10+ Year Member



I'm not looking for someone to write my code, I just need some advice because as much reading as I can do I'll never be an expert in this area. I had gone through most of the posts on this forum and none of them seemed to work. So I sat down and did some research of my own and came up with this that seems to work

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

jdMorgan

2:15 pm on Jan 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteRule ([^/]+)\.html?$ /test.php?user=%1&page=$1 [L]

This rule accepts URL formats of either xyz.html or xyz.htm, and will return the same content for either. This creates 'duplicate content' and can result in search engines splitting the PageRank/Link-popularity of a page across the two URLs if links are found to both.

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]

This will accept only requests for URLs that appear to resolve to the top-level directory of each user, contain only one "." and end in html or html.

Jim

test1

4:35 pm on Jan 3, 2008 (gmt 0)

10+ Year Member



Thanks, that all makes sense. I've modified the script so that it only accepts .htm and added the modifications you posted and everything seems to be working fine now. The site hasn't been launched and all the internal links are generated by scripts so it's pretty easy to change. Thanks for the help.

phpprogrammer

6:32 am on Feb 11, 2008 (gmt 0)

10+ Year Member



Hi test1,

Can i have the list of changes which you have done. I want todo same type of things in my local setup.

Thanks in advance.

test1

6:19 pm on Feb 11, 2008 (gmt 0)

10+ Year Member



Well since you can't really setup wildcard subdomains locally without some sort of DNS server all I did was add the subdomain address to my hosts file, like username.localhost. Then it worked enough for my testing purposes.

phpprogrammer

4:05 am on Feb 12, 2008 (gmt 0)

10+ Year Member



Thanks. for help. Just want to confirm the setup of wildcard subdomains is possible on live server.

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.

jdMorgan

5:21 pm on Feb 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first rewritecond isn't necessary, since "localhost" won't match your second rewritecond anyway.

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]

Your rewriterule pattern, as written, will only accept .htm or .html files in your top-level directory as a match.

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

phpprogrammer

4:20 am on Feb 13, 2008 (gmt 0)

10+ Year Member



Thanks Jim for the help. It's worked. I am near to the solution which i want. Can you help me more in one point that mentioned below?

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.

jdMorgan

5:52 pm on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can modify the rule and pattern to back-reference any and all parts of the requested URL-path. See the Apache mod_rewrite documentation.

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