Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite. Ugh.

didn't know it was this complicated to understand...

         

xcandace

8:01 pm on May 29, 2009 (gmt 0)

10+ Year Member



Hi I am trying to use the mod_rewrite function on my website to forward [username.mydomain.com...] to [mydomain.com...]

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]

g1smd

8:42 pm on May 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need a set of redirects to redirect all requests for all alternative domains to your canonical domain. Failure to do that leads to Duplicate Content indexing. The redirect will be a 301 redirect and will force www at the same time within the same redirect. It will also preserve the requested path part in the redirect.

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.

jdMorgan

9:50 pm on May 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you care about search rankings the first thing to do is to canonicalize your domain name... You *do not* want all of those TLD flavors to resolve directly to content (See Google Forum thread titled "Duplicate Content -- Get it Right or Perish"). Instead, 301-redirect all of the 'alternate' TLD hostnames to one canonical domain, and link to and promote only that one hostname on-line. Otherwise, you're essentially setting up multiple domains all in direct competition with each other, and none may rank well...

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]

Note that you should accepts only restricted usernames in order to enforce HTTP compliance, to prevent security and duplicate-content problems, and to preserve your sanity. The username should be in the format <any lowercase letter> <any reasonable number of lowercase letters or numbers, including zero> <optional single hypen> <one or more lowercase letters or numbers, within reason>.

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