Forum Moderators: phranque

Message Too Old, No Replies

.htaccess and subdomains

         

gosman

2:15 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



How do I set my server up so I can have a sperate .htaccess file for each subdomain.

I have 2 hosts

www with a DocumentRoot "/var/www/"

and

news with a DocumentRoot "/var/www/sub_news"

I have the following in my .htaccess file for www

Options +FollowSymlinks -Indexes
RewriteEngine On

AddDefaultCharset UTF-8
AddType application/x-httpd-php .php .htm .html

RewriteCond %{HTTP_HOST} ^example.com [NC]
rewriterule ^(.*)$ http://www.example.com/$1 [L,R=301]

ErrorDocument 404 /errorpage.htm

rewriterule ^(.*)/(.*)/(.*)/index.htm http://www.example.com/$1/$2/$3/ [L,R=301]
rewriterule ^(.*)/(.*)/(.*)/$ /cms/page.php?GROUP_ID=$1&PID1=$3&PID2=$2

rewriterule ^(.*)/(.*)/index.htm http://www.example.com/$1/$2/ [L,R=301]
rewriterule ^(.*)/(.*)/$ /cms/page.php?GROUP_ID=regions&PID1=$2&PID2=$1

rewriterule ^(.*)/index.htm http://www.example.com/$1/ [L,R=301]
rewriterule ^(.*)/$ /cms/page.php?GROUP_ID=countries&PID1=$1

The subdomain stops working when I enable the .htaccess in www

Any help would be really appreciated

[edited by: jdMorgan at 3:59 pm (utc) on Sep. 9, 2008]
[edit reason] example.com [/edit]

jdMorgan

3:57 pm on Sep 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to exclude /cms/page.php from being rewritten to itself (using a RewriteCond with a negative-match pattern), and put your redirects in order from most-specific to least-specific, followed by your internal rewrites, again in order from most-specific to least-specific. For example, your domain canonicalization redirect should be the last redirect.

The ".*" pattern is ambiguous and greedy, and leads to extremely-inefficient pattern-matching. I strongly suggest you use more-specific patterns whenever possible. For example:


rewriterule ^(.*)/(.*)/(.*)/$ /cms/page.php?GROUP_ID=$1&PID1=$3&PID2=$2

can be much more efficiently and specifically coded as

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=$1&PID1=$3&PID2=$2

Jim

jdMorgan

4:05 pm on Sep 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, in order to prevent looping, all index.htm redirects of this form:

rewriterule ^(.*)/(.*)/(.*)/index.htm http://www.example.com/$1/$2/$3/ [L,R=301]

can and should be replaced with a single redirect like this:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]

This one rule will properly handle index.htm requests in *any* subdirectory.

I also suggest that you debug these rule-sets one at a time, rather than trying to test and debug them all at once. Divide and conquer -- simplify.

Jim

gosman

4:46 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



Thank you for your help Jim. I'm new to this .htaccess lark.

I've changed the code as suggested and it works fine and I'm sure more efficiently, however the subdomain problem still exists.

Here is the modified code I'm using.

Options +FollowSymlinks -Indexes
RewriteEngine On

AddDefaultCharset UTF-8
AddType application/x-httpd-php .php .htm .html

RewriteCond %{HTTP_HOST} ^example.com [NC]
rewriterule ^(.*)$ http://www.example.com/$1 [L,R=301]

ErrorDocument 404 /errorpage.htm

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=$1&PID1=$3&PID2=$2
RewriteRule ^([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=regions&PID1=$2&PID2=$1
RewriteRule ^([^/]+)/$ /cms/page.php?GROUP_ID=countries&PID1=$1

encyclo

4:49 pm on Sep 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How do I set my server up so I can have a sperate .htaccess file for each subdomain

Are we talking about a dedicated server? If so, then move the subdirectory files to a different location outside the document root of the primary domain. There is no need to use the subdomain-as-directory tactic, and several disadvantages. Separate document roots mean separate rules for each with no interference.

jdMorgan

5:07 pm on Sep 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're not on a dedicated or virtual private server and can't use the option suggested above, you may need to explicitly exclude the subdomain-subdirectory path requests from being rewritten. Also, you still have the redirects in the wrong order, specificity-wise. Correcting that and cleaning-up a few other minor points:

Options +FollowSymlinks -Indexes
RewriteEngine on
#
AddDefaultCharset UTF-8
AddType application/x-httpd-php .php .htm .html
ErrorDocument 404 /errorpage.htm
#
# Externally redirect direct client requests for <any_directory>/index.htm to <same_directory>/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect to canonical www domain
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com(\.¦:[0-9]+) [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Skip the following three rules if subdomain-subdirectory path requested
RewriteRule ^subdomain_directory_path/ - [S=3]
#
# Internally rewrite SE-friendly URLs to CMS script
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=$1&PID1=$3&PID2=$2
RewriteRule ^([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=regions&PID1=$2&PID2=$1
RewriteRule ^([^/]+)/$ /cms/page.php?GROUP_ID=countries&PID1=$1

Change the broken pipe "¦" character in the domain canonicalization rule to a solid pipe character before use; Posting on this forum modifies the pipe characters.

Jim

gosman

5:30 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



Thank guys.

It is a dedicated server so I was able to move the subdirectory files to a different location outside the document root of the primary domain as suggested by Encyclo. This has now cured the sub-domain problem.

I have also cleaned up the code as suggested by Jim. Here it is

Options +FollowSymlinks -Indexes
RewriteEngine On

AddDefaultCharset UTF-8
AddType application/x-httpd-php .php .htm .html

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com [NC]
rewriterule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=$1&PID1=$3&PID2=$2
RewriteRule ^([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=regions&PID1=$2&PID2=$1
RewriteRule ^([^/]+)/$ /cms/page.php?GROUP_ID=countries&PID1=$1

As usual, thanks for all your help guys.

jdMorgan

1:59 am on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You missed a few changes -- Please review.

Jim

gosman

8:38 am on Sep 12, 2008 (gmt 0)

10+ Year Member



Thanks Jim

Here's what I have now

Options +FollowSymlinks -Indexes
RewriteEngine On

AddDefaultCharset UTF-8
AddType application/x-httpd-php .php .htm .html

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com(\.¦:[0-9]+) [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=$1&PID1=$3&PID2=$2
RewriteRule ^([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=regions&PID1=$2&PID2=$1
RewriteRule ^([^/]+)/$ /cms/page.php?GROUP_ID=countries&PID1=$1

It seems to be working fine, does it look correct?

jdMorgan

4:14 pm on Sep 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks OK, except that I forgot to add [L] flags to your last three (CMS) rules, and this should be corrected before proceeding. But you need to test each case handled by the rules:

Any request for index.html in any subdirectory should be redirected to "/" in that subdirectory.

Requests for example.com/, www.example.com./, www.example.com:80/, or www.example.com.:80/ should be redirected to www.example.com/

And then of course check that your CMS rewrites are working properly.

Jim