Welcome to WebmasterWorld Guest from 18.208.187.169

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Subdomain

     
7:45 pm on Mar 7, 2011 (gmt 0)

New User

5+ Year Member

joined:Dec 7, 2010
posts: 35
votes: 2


Hi 2 all,
I did some research here but don't have luck...
I created subdomain on my VDS (mobile.example.com) and that give me the folder in website root: mobile.
First, it redirected to http://example.com/mobile so I put this in .htaccess:

RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.example\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}//%1 -d
RewriteRule ^(.*) mobile/%1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]

and that give me proper url in address bar: [mobile.example.com,...] but my regular links stop working and returned me 404! (like: http://example.com/media/somepage.html)
What I am doing wrong?
6:24 pm on Mar 9, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


What is the intended usage of the REDIRECT_SUBDOMAIN and SUBDOMAIN variables? Please document that in the code to clarify the whole situation.

It is also not clear why "First, it redirected to http://example.com/mobile" -- What caused the redirect, your code or something else? This certainly should not happen unless you coded something to cause it to happen.

Your code *will* rewrite requests for www.example.com/xyz to /mobile/www/xyz, but it won't rewrite requests for just example.com/xyz... the subdomain label is required by your rewritecond.

Jim
2:39 pm on Mar 12, 2011 (gmt 0)

New User

5+ Year Member

joined:Dec 7, 2010
posts: 35
votes: 2


This is my current .htaccess:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
<Files .htaccess>
order allow,deny
deny from all
</Files>
#
Options -Indexes
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /itemout\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^itemout\.php$ /filepath-that-is-known-not-to-exist.xyz [L]
#
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
#
RewriteRule ^media/([^/.]*)/([A-Za-z0-9-]+).html$ itemout.php?id=$1 [L]
#
ErrorDocument 404 /index.php?action=p404
---------------------
So, I have folder mobile in the root of my site (/public_html/mobile) where the main files for subdomain mobile are stored.
I want the mobile.example.com give me same result in the address bar, right now it give me: example.com/mobile.
11:56 pm on Mar 17, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


First, change all links on your site to point to mobile.example.com instead of to example.com/mobile/
Then change your .htaccess file:

ErrorDocument 404 /index.php?action=p404
#
Order deny,allow
<Files .htaccess>
Deny from all
</Files>
#
Options +FollowSymlinks -Indexes
#
RewriteEngine on
RewriteBase /
#
# Externally redirect direct client requests for itemout.php to
# non-existent filepath to force 404-Not Found response
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /itemout\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^itemout\.php$ /filepath-that-is-known-not-to-exist.xyz [L]
#
# Externally redirect direct client requests for /mobile subdirectory
# to non-existent filepath to force 404-Not Found response
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /mobile/[^\ ]*\ HTTP/
RewriteRule ^mobile/ /filepath-that-is-known-not-to-exist.xyz [L]
#
# Externally redirect non-blank non-canonical mobile hostname requests to canonical mobile.example.com
# hostname. (Also accept and handle requests for the very-common mobile hostname hostname "m.example.com"
# as well as "www.m.example.com", "m.www.example.com", etc.)
RewriteCond %{HTTP_HOST} ^([^.]+\.)*(mobile|mobi|m)\. [NC]
RewriteCond %{HTTP_HOST} !^mobile\.example\.com$
RewriteRule ^(.*)$ http://mobile.example.com/$1 [R=301,L]
#
# Externally redirect non-blank non-mobile non-canonical hostname requests to canonical example.com hostname
RewriteCond %{HTTP_HOST} !^((mobile\.)?example\.com)?$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#
# Internally rewrite mobile subdomain requests to /mobile subdirectory unless already done
RewriteCond %{HTTP_HOST} ^mobile\.example\.com$
RewriteCond $1 !^mobile/
RewriteRule ^(.*)$ /mobile/$1 [L]
#
# Internally rewrite main site /media/ requests to itemout.php script
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^media/([^/.]*)/([A-Za-z0-9\-]+)\.html$ itemout.php?id=$1 [L]

Again, do not use this code until you have corrected all the mobile links on your site. The code above will return a 404 if any attempt is made by the client to load anything directly from the /mobile subdirectory.

Also, do yourself a really big favor -- leave the comments in-place. The code is fairly efficient but somewhat complex as a result, and so should be well-documented.

Rule order is critical, except that you may swap the last two rules if you wish. For best performance, put the one that will execute most often first (check your stats and compare traffic to those two paths). If not sure, don't worry about it - the performance difference will be tiny, but not zero.

You will need to put separate robots.txt, sitemap.xml, labels.rdf, crossdomain.xml, and /w3c/p3p.xml files into the /mobile subdirectory if you wish to use these hostname-specific features for your mobile.example.com site.

As suggested by the mobile hostname redirect rule above, you may wish to consider supporting the very-common "m.example.com" and "mobi.example.com" mobile hostnames (set up your DNS record to allow them). The "m.example.com" and "mobi.example.com" hostnames are quite common for mobile sites because they are short, and if the user is trying to type in a URL on a phone without a real keyboard, then short is good (or at least it should be allowed).

Jim