Forum Moderators: phranque
I am trying to think ahead before my head explodes trying to get it to work. I have more than one domain pointing to my website...
--- I own the following of my domain
.com
.net
.org
.info
I want the subdomain (username) to work for all extensions basically. I have been trying to learn/understand this for about 2 hours now but I can't grasp it. This is what I have so far but im not sure if its gonna work (because of the .com in there I am also assuming that it in no way shape or form covers the other domain extensions). Please help :(
RewriteEngine on
RewriteCond %{HTTP_HOST} ^username\.example\.com [NC]
RewriteRule .* /profile.php?user=username [R=301,L]
Next, you need a rewrite to accept the canonical URL request and fetch the file content that you want to 'associate' with that URL request. This rule must NOT contain a domain name, and must use the [L] flag. It needs to be called only for direct client requests.
Optionally, you need another redirect such that if anyone asks for any sort of URL using parameters, they are redirected to the correct and canonical form of the URL, and the www is also forced at the same time.
All of these things have been covered at least once per week since the forum started life. You first code has 'part' of what you need to do for the rewrite, but coded such that it forces an unwanted redirect.
Some points here:
- The redirect to canonical domain needs to include the canonical domain name in the target URL and use the [R=301,L] flags.
- The rewrite from external URL to internal file needs only [L] and no domain name within.
- The .* pattern is too greedy. It will rewrite requests for /robots.txt too. Use a more restrictive pattern.
- RewriteRule cannot see parameters, so for your rule that redirects parameter-based URLs to the new format, use a RewriteCond looking at QUERY_STRING, to get the user name.
- Use back-references to capture variable information and re-use it, so that you don't have to code for each individual user name.
There's three distinct steps here. Three separate rules. Two of them are redirects and the final one is a rewrite.
Taking all of that into account:
RewriteEngine on
#
# Externally redirect "www" subdomain requests to canonical non-www hostname
RewriteCond %{HTTP_HOST} ^www\.example\. [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#
# Externally redirect non-canonical TLDs (and all non-www subdomains) to canonical TLD
RewriteCond %{HTTP_HOST} !^([^.]+\.)*example\.com$
RewriteCond %{HTTP_HOST} ^(([^.]+\.)*)example\. [NC]
RewriteRule ^(.*)$ http://%1example.com/$1 [R=301,L]
#
# Internally rewrite <username>.example.com requests to /profile.php?user=<username>,
# retaining any other query string data
RewriteCond $1 !^profile\.php
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^(.*)$ /profile.php?user=%1 [QSA,L]
For example, checking usernames using a pattern of "^([a-z][a-z0-9]{0,7}-?[a-z0-9]{1,8})$", a valid username will consist of at least two characters, but not more than 17 (with optional hyphen), may have one hyphen in the middle, and will comply with HTTP subdomain-naming requirements.
You also need to consider images, multimedia files, and 'special' files such as sitemap.xml and robots.txt. Do you really want to rewrite requests for all of these to your script? And as-coded above, each subdomain can consist of only one 'page' unless you check REQUEST_URI within your script.
Jim