Forum Moderators: phranque
This is the first time, I am doing this.
In my member site, I want to assign all members an unique url for easy navigation. Some thing like this: [impact.domain.com ]
The url: [impact.domain.com ] will get redirected here
[domain.com ]
-----------------------------------------
Now let me tell you this in detail. Initial steps are standard, where members fill in a form for registration. Activation email is sent. Once they activate the script checks database for domain name. If the domain field is null then it assumes that the user is new and proceeds for domain check up. The user chooses a domain name. The scripts then creates a folder at the following location
[domain.com...] domain> and copy's a index.php file from another location.
So here is what I got
-------------------------
Options +FollowSymlinks
RewriteEngine On
# Extract the subdomain part
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
# Verify subdomain is not www
RewriteCond %{HTTP_HOST} !^www [NC]
# Check if the directory actually exists before we go there
RewriteCond /home/domain/public_html/members/%1 -d
# Condition when directory does not exist
# [domain.com...] domain>
--------------------------
I have no clue how to redirect a user when there is no such sub domain or members registered with us.
I also want to replace this:
[DOMAIN.COM ]
with this:
[<sub domain>.domain.com ]
I have a goddady.com shared hosting and wild card for subdomain is already enabled.
Does any one knows how to get this done? Any help will be really appreciated.
Thank you,
Options +FollowSymlinks
RewriteEngine on
#
# (2) Make sure we haven't already rewritten this request
RewriteCond $1 !^members/
# Extract the subdomain name
RewriteCond %{HTTP_HOST} ^([a-z][a-z0-9\-]*[a-z0-9]+)\.example\.com
# Verify that subdomain is not www
RewriteCond %1 !=www [NC]
# Verify that the username-subdirectory actually exists
RewriteCond %{DOCUMENT_ROOT}/members/%1 -d
# Internally rewrite to user-subdomain subdirectory
RewriteRule ^(.*)$ /members/$1 [L]
#
# (3) Else we've already done the rewrite, the requested subdomain
# was "www" or blank, or the subdirectory does not exist
# Extract the subdomain name
RewriteCond %{HTTP_HOST} ^([a-z][a-z0-9\-]*[a-z0-9]+)\.example\.com
# Verify that subdomain is not www
RewriteCond %1 !=www [NC]
# Redirect to "create account" unless already done
RewriteRule !^members/ http://www.example.com/error-you-must-create-an-account.html [R=302,L]
Furthermore, there may be no need to copy the index.php file to each user's subdirectory. You can exclude request for "/index.php" and "/" from being rewritten by adding
RewriteCond $1 !^(index\.php)?$ This may require a change to the code in the index.php file, to 'get' the username from the requested subdomain instead of taking it from the requested URL-path, but this is fairly simple.
---
You may also want a third rule for added security -- to prevent user1 from typing in example.com/members/user2 in order to 'peek' at other "user accounts." Add this before the first two rules above:
# (1) Redirect direct client requests for user subdirectories back to user subdomains
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /members/[a-z0-9\-]+(/[^\ ]*)?\ HTTP/
RewriteRule ^members/([a-z][a-z0-9\-]*[a-z0-9]+)(/(.*))?$ http://$1/example.com/$3 [R=301,L]
# (4) Externally redirect requests for non-canonical user subdomains (e.g. user
# subdomain requests containing prohibited characters or uppercase characters)
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteCond %{HTTP_HOST} !^[a-z][a-z0-9\-]*[a-z0-9]+\.example\.com
RewriteRule ^ http://www.example.com/invalid-username-perhaps-misspelled-or-uppercase.php [R-302,L]
#
# (5) Externally redirect requests for "example.com" or any non-canonical
# variation of www.example.com to canonical "www.example.com"
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R-301,L]
Do not allow any characters other than lowercase letters, digits zero through nine, and hyphens in usernames. Doing so could cause problems in subdomain-to-subdirectory mapping as previously discussed, and would allow the creation of subdomains with character that are not allowed in HTTP domain names. This is why I've made each line above very specific as to what characters it will accept, including the use of the [NC] flag where it appears.
To be clear, a subdomain must start with a character, followed by characters, numbers, or hyphens, and end with a character or number.
I just typed this. It has not been tested. There may be typos or logical errors. I present it as an example, not as a solution. No warranty is expressed or implied. :)
To make life easy, I'd suggest testing the rules in the order discussed, adding on rule at a time after the first two, and not trying to test them all at once to begin with.
Don't use any of this code until you analyzed it using the mod_rewrite and regular-expressions documantation and understand it completely. To do so might be very dangerous to the health and success of your site. Changing the code without thoroughly understanding might be dangerous to the security of your site.
Jim
I really appreciate your help.
When I decided to do this, I searched on the net. I found few links to this site where the user wanted to redirect the subdomain in this manner
[<sub>.domain.com...] to [domain.com...]
I think from SEO point of view my idea would be better.
Thank you and I will check your code by running it on my server.
Impact
Jim