Newbie Question

Using a single web hosting account to host multiple sites

Newbie Question

Postby weewah » Fri Jun 12, 2009 6:52 am

I have the following requirement:
i. http://pattern1.domain.com => http://pattern.domain.com/xxx/yyy?page=pattern1

ii. http://www.domain.com/pattern2 => http://www.domain.com/abc/def?type=pattern2

Can anyone share how the above can be done? Thank you in advance.

note 1: pattern1 is alphabets, pattern2 can contain alphabet, numbers, spaces, hyphens, underscore, dots and &.

note 2: http://www.domain.com is redirected to the home page through the index.html in the home folder.

note 3: the above is for a virtual url.
weewah
 
Posts: 20
Joined: Fri Jun 12, 2009 6:37 am

Postby richardk » Fri Jun 12, 2009 8:42 am

For the first part. FAQ: Virtual sub domains.

The sub domains need DNS. The server needs to be configured to send the sub domains to the directory containing the .htaccess file.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z]+)\.example\.com$ [NC]
RewriteRule ^$ /xxx/yyy?page=%2 [QSA,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([-a-z0-9\ _.&]+)/?$ /abc/def?type=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby weewah » Sun Jun 14, 2009 5:30 pm

Thanks for your reply.

Yes, I already have the subdomains configured in the DNS. I'm using the virtual host in httpd.conf for the configuration, not .htaccess file. Does your code for for that?

At the same time, both the above criteria needs to be fulfilled.

Anyone have any ideas?
weewah
 
Posts: 20
Joined: Fri Jun 12, 2009 6:37 am

Postby richardk » Mon Jun 15, 2009 11:27 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z]+)\.example\.com$ [NC]
RewriteRule ^/$ /xxx/yyy?page=%2 [QSA,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^/([-a-z0-9\ _.&]+)/?$ /abc/def?type=$1 [QSA,L]


If it doesn't work post your <VirtualHost>.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby weewah » Mon Jun 22, 2009 9:29 pm

Can you explain how the below works line by line? Thanks.

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z]+)\.example\.com$ [NC]
RewriteRule ^/$ /xxx/yyy?page=%2 [QSA,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^/([-a-z0-9\ _.&]+)/?$ /abc/def?type=$1 [QSA,L]
weewah
 
Posts: 20
Joined: Fri Jun 12, 2009 6:37 am

Postby richardk » Tue Jun 23, 2009 12:19 pm

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Make sure it's not (!) a request to www.exmaple.com.
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
# If it is a sub domain request (either www.sub.example.com or sub.example.com)
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z]+)\.example\.com$ [NC]
# and it is a request for / (the site's root) rewrite the request to /xxx/yyy?page=sub.
RewriteRule ^/$ /xxx/yyy?page=%2 [QSA,L]

# If it's not a request to an existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# and if it's not a request to an existing directory
RewriteCond %{SCRIPT_FILENAME} !-d
# and it matches "pattern1" rewrite to /abc/def?type=pattern1.
RewriteRule ^/([-a-z0-9\ _.&]+)/?$ /abc/def?type=$1 [QSA,L]


pattern1
alphabets, pattern2 can contain alphabet, numbers, spaces, hyphens, underscore, dots and &
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby weewah » Tue Jun 23, 2009 11:22 pm

Thanks for your reply!

I've modified the below to suit my needs... what does %1 do? Any difference from $1?

Code: Select all
# Make sure it's not (!) a request to www.abc.com.sg.
RewriteCond %{HTTP_HOST} !^www\.abc\.com\.sg$ [NC]
# If it is a sub domain request (sub.abc.com.sg)
RewriteCond %{HTTP_HOST} ^([a-z]+)\.abc\.com\.sg$ [NC]
# and it is a request for / (the site's root) rewrite the request to /xxx/yyy?page=sub.
RewriteRule ^/$ /xxx/yyy?page=%1 [QSA,L]


The Apache web server that I'm doing the mod rewrite has a plugin module that redirects traffic to an application server. Wonder if the second part of the rewrite rule will cause it to fail?
weewah
 
Posts: 20
Joined: Fri Jun 12, 2009 6:37 am

Postby weewah » Wed Jun 24, 2009 12:50 am

I need to implement the second requirement first, so I added the below code into the web server httpd.conf. However, when I accessed http://www.example.com/index.html, it is also redirected. Shouldn't it be caught by the first rewrite condition as it is an existing filename? I expect to see the contents of index.html.

Code: Select all
# If it's not a request to an existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# and if it's not a request to an existing directory
RewriteCond %{SCRIPT_FILENAME} !-d
# and it matches "pattern1" rewrite to /abc/def?type=pattern1.
RewriteRule ^/([-a-z0-9\ _.&]+)/?$ /abc/def?type=$1 [QSA,L]


What does QSA mean? My code only worked when I used [R=301,L] in place of that as the redirected page is in the application server.

Thanks.
weewah
 
Posts: 20
Joined: Fri Jun 12, 2009 6:37 am

Postby richardk » Wed Jun 24, 2009 8:59 am

I've modified the below to suit my needs... what does %1 do? Any difference from $1?

$n is from the RewriteRule. %n is from the last RewriteCond (that creates backreferences/variables).

The Apache web server that I'm doing the mod rewrite has a plugin module that redirects traffic to an application server. Wonder if the second part of the rewrite rule will cause it to fail?

If you add the PT (Pass Through) flag (replace [QSA,L] with [QSA,PT,L]) the other module should be processed as well.

I need to implement the second requirement first, so I added the below code into the web server httpd.conf. However, when I accessed http://www.example.com/index.html, it is also redirected. Shouldn't it be caught by the first rewrite condition as it is an existing filename? I expect to see the contents of index.html.

My mistake, in the httpd.conf file %{SCRIPT_FILENAME} does not contain the document root. You need
Code: Select all
%{DOCUMENT_ROOT}%{SCRIPT_FILENAME}

instead of just
Code: Select all
%{SCRIPT_FILENAME}


What does QSA mean?

Query String Append. If you request /jkl?mno=pqr it would rewrite to /abc/def?type=jkl&mno=pqr. Without it it would go to /abc/def?type=jkl. More information about RewriteRule flags.

My code only worked when I used [R=301,L] in place of that as the redirected page is in the application server.

The PT flag should sort that.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby weewah » Wed Jun 24, 2009 5:37 pm

Thanks. I think it works. ;) Just to make sure I got the code correct...

Code: Select all
RewriteEngine on
# If it's not a request to an existing file
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
# and if it's not a request to an existing directory
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
# and it matches "pattern1" rewrite to /abc/def?type=pattern1.
RewriteRule ^/([-a-z0-9\ _.&]+)/?$ /abc/def?type=$1 [QSA,PT,L]


Do you have more information on %{DOCUMENT_ROOT}? I was wondering what other things should be checked. In terms of usage, when do I use %{DOCUMENT_ROOT} or %{SCRIPT_FILENAME}?
weewah
 
Posts: 20
Joined: Fri Jun 12, 2009 6:37 am

Next

Return to Domain Handling

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron