Forum Moderators: phranque

Message Too Old, No Replies

How to add a subdomain in htaccess?

         

pharaon2222

1:19 am on Apr 27, 2009 (gmt 0)

10+ Year Member



Hi, guys

I've a htaccess file that works well perfectly for my site. Several days ago I started to work on the mobile version of my site, and to place it on a subdomain, like mobile.example.com.

I tried to find some info on Internet about adding subdomains in htaccess, and no one of added codes worked. Please, have a look on my code and propose a solution.

Thank you.

RewriteEngine On
RewriteBase /
RewriteRule ^.htaccess$ - [F]

ErrorDocument 404 /index.php?module=404

RewriteRule ^.*/img/(.+)$ img/$1 [L]
RewriteRule ^.*/cms/(.*) img/$1 [L]
RewriteRule ^img/(.*) img/$1 [L]
RewriteRule ^img img/ [L]
RewriteRule ^.*/(.+).css$ $1.css [L]
RewriteRule ^.*/(.+).js$ $1.js [L]
RewriteRule ^.*/(.+).php$ $1.php [L]

RewriteRule ^$ index.php [L]
RewriteRule ^/$ index.php [L]

RewriteRule ^ro/$ ?Lang=ro [L]
RewriteRule ^en/$ ?Lang=en [L]
RewriteRule ^ru/$ ?Lang=ru [L]
RewriteRule ^ro$ ?Lang=ro [L]
RewriteRule ^en$ ?Lang=en [L]
RewriteRule ^ru$ ?Lang=ru [L]

RewriteRule ^(.+)/Regions/(.+)/$ index.php?module=main&dat=$2 [L]

RewriteRule ^(.+)/about/$ index.php?module=page [L]
RewriteRule ^(.+)/Contacts/$ index.php?module=contacts [L]
RewriteRule ^(.+)/Recommend/$ index.php?module=recommend [L]

jdMorgan

4:24 pm on Apr 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is likely that you will have to check the %{HTTP_HOST} variable to see which hostname is being requested, and also to check %{HTTP_USER_AGENT} and/or %{ACCEPT} in order to determine whether the device is a mobile device. If the mobile subdomain is being requested, rewrite the URL-path to the subfolder where your mobile content is stored. If the main domain is being requested by a mobile device, redirect the request to your mobile subdomain.

You may also want to provide a cookie-based override, so that users of advanced mobile devices such as iPhones and Blackberrys can override the main-to-mobile redirect if they prefer to 'pan and scan' your main site instead of using your mobile site.

Implementing a subdomain requires several steps. First, the subdomain (or a wild-card) must be defined in your DNS zone file, as provided by your domain registrar. Second, your hosting service must provide you with the capability to create "add-on domains," or you must have a unique IP address for your server (which would allow it to host *any* domains or subdomains that you might wish to point at it using your DNS zone file. Finally, in this latter case, you must add configuration code on your server to 'sort out' requests for those various domains and subdomains and point each of them to the correct file space on the server.

Note that the code you posted above can be replaced with only ten lines instead of 23. We can discuss that some more once your main problem is solved.

Jim