Forum Moderators: phranque
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]
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
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /cms/page.php?GROUP_ID=$1&PID1=$3&PID2=$2
rewriterule ^(.*)/(.*)/(.*)/index.htm http://www.example.com/$1/$2/$3/ [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]
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
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
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.
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
Jim
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.
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?
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