Forum Moderators: phranque

Message Too Old, No Replies

mod rewirite: specified subdomain to a specified file

         

miksson

10:34 pm on Jan 25, 2011 (gmt 0)

10+ Year Member



Hi,

what I want is to point specified subdomains to a specified file with parameters using htaccess, like this:

(www.)first.domain.com --> domain.com/index.php?call=row&id=1
(www.)second.domain.com --> domain.com/index.php?call=row&id=2

(www.)john.domain.com --> domain.com/index.php?call=name&id=1
(www.)amy.domain.com --> domain.com/index.php?call=name&id=2

I don't need wildcards, I will add subdomains and redirects by hand - I will have about 70 such a redirects. The rewrite should not affect the address bar.

I tried many times using examples from this forum posts, but no luck. Can you help me?

g1smd

11:11 pm on Jan 25, 2011 (gmt 0)

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



What code did you try so far?

miksson

11:23 pm on Jan 25, 2011 (gmt 0)

10+ Year Member



I did many approaches. Now I have in my htaccess, but it's not working:

Options +FollowSymlinks

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /


RewriteCond %{HTTP_HOST} ^first\.domain\.com [NC]
RewriteRule ^ index.php?call=row&id=1 [QSA,L]

g1smd

12:17 am on Jan 26, 2011 (gmt 0)

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



You have no RewriteRule associated with the first two RewriteCond lines.

miksson

1:07 am on Jan 26, 2011 (gmt 0)

10+ Year Member



I commented them and it started to work now, thanks!

I have a problem with css, scripts and images now. To make them work/appear under first.domain.com, I have to link them like: <img scr="http://domain.com/images/sun.jpg"> instead of just <img scr="/images/sun.jpg">
Is there a way to set it up correctly in htaccess?

jdMorgan

8:14 pm on Jan 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Linking to them using server-relative links can be moade to work if you exclude the /images path from being rewritten to your index.php script. Do the same thing for css and JavaScript included objects as well.

Jim

jdMorgan

6:52 pm on Feb 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Example:

RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.domain\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com(\.?:[0-9]+|\.)$ [NC]
RewriteRule ^(.*)$ http://%1.domain.com/$1 [R=301,L]
#
RewriteCond $1 !(^index\.php|\.(gif|jpe?g|png|ico|css|js|pdf|doc|xml|txt))$
RewriteCond %{HTTP_HOST}>1 ^first\.domain\.com>(.+)$ [NC,OR]
RewriteCond %{HTTP_HOST}>2 ^second\.domain\.com>(.+)$ [NC,OR]
RewriteCond %{HTTP_HOST}>3 ^john\.domain\.com>(.+)$ [NC,OR]
RewriteCond %{HTTP_HOST}>4 ^amy\.domain\.com>(.+)$ [NC]
RewriteRule ^(.*)$ index.php?call=row&id=%2 [QSA,L]

The now-second rule excludes the listed files and filetypes from being rewritten, and condenses all subdomain-to-numbered-script-parameter rewriting to a single rule. If the "exclude files and filetypes" approach proves cumbersome, then you may wish to use a positive-match rule to rewrite only specified types of requested URLs to your script.

The new first rule simply forces a redirect to canonical hostnames if non-canoncal hostnames are requested.

Jim