views:

92

answers:

1

Hi all,

I use htaccess with a set of rules to control the different pages of my website.

  1. If a user visits my main site (domain.com), without using www it 301 redirects to www.domain.com (for SEO purposes)
  2. I use wildcard subdomains, so if a user visits subdomain.domain.com, they will view a specific PHP file that decides to content for them.

My problem is that I would like to extend my setup, to be able to use the same url names. As an example if I visit subdomain.domain.com/feed, I will see pages/public_news_feed.php file from the main domain rule, since it shared the "/feed" rule. What I really wanted to be served is pages/public_subdomain_blog_feed.php!

How can I setup specific rules for domain.com and subdomain.domain.com?

Thanks in advance and have a wonderful day!

#AuthName "Restricted Area" 
#AuthType Basic
#AuthUserFile /var/www/user/data/www/domain.com/.htpasswd 
#require valid-user

Options +FollowSymLinks
RewriteEngine on

#force domain setup to use www
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

#redirect subdomains to controller
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^$ pages/public_subdomain_blog.php [L,QSA]

RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(indsend-nyhed)$           pages/public_submit_news.php [L]
RewriteRule ^(modeblogs)$               pages/public_blogs.php [L]
RewriteRule ^(nyheder)$             pages/public_news.php [L]
RewriteRule ^(feed)$                    pages/public_news_feed.php [L]
RewriteRule ^(nyheder)/([0-9]*)/(.*)$       pages/public_news_single.php?n_id=$2 [L]
RewriteRule ^(kontrolpanel)$                pages/private_account.php [L,QSA]
RewriteRule ^(kontrolpanel)/(logud)$            pages/private_logout.php [L]


## THESE RULES SHOULD ONLY BE SET IF THE USERS IS ON A SUBDOMAIN
RewriteRule ^([0-9]*)/(.*)$         pages/public_subdomain_blog_article.php?p_id=$1 [L]
RewriteRule ^(kategori)/([0-9]*)$       pages/public_subdomain_blog.php?c_id=$2 [L]
RewriteRule ^(arkiv)/([0-9]*)$      pages/public_subdomain_blog.php?a_id=$2 [L]
RewriteRule ^(feed)$                pages/public_subdomain_blog_feed.php [L]
+1  A: 

Well, you already know how to condition on whether the user is on a subdomain, since you do it for your controller already:

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^$ pages/public_subdomain_blog.php [L,QSA]

While it may or may not be possible to simplify your entire ruleset a bit, the easiest thing to do is to just condition your rules in the same way. Since you have fewer subdomain rules, we can move those above your main site rules and apply the rewrites conditionally, using the condition you have:

## THESE RULES SHOULD ONLY BE SET IF THE USERS IS ON A SUBDOMAIN
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^([0-9]*)/(.*)$       pages/public_subdomain_blog_article.php?p_id=$1 [L]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(kategori)/([0-9]*)$ pages/public_subdomain_blog.php?c_id=$2 [L]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(arkiv)/([0-9]*)$    pages/public_subdomain_blog.php?a_id=$2 [L]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(feed)$              pages/public_subdomain_blog_feed.php [L]

RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(indsend-nyhed)$         pages/public_submit_news.php [L]
RewriteRule ^(modeblogs)$             pages/public_blogs.php [L]
RewriteRule ^(nyheder)$               pages/public_news.php [L]
RewriteRule ^(feed)$                  pages/public_news_feed.php [L]
RewriteRule ^(nyheder)/([0-9]*)/(.*)$ pages/public_news_single.php?n_id=$2 [L]
RewriteRule ^(kontrolpanel)$          pages/private_account.php [L,QSA]
RewriteRule ^(kontrolpanel)/(logud)$  pages/private_logout.php [L]
Tim Stone
I was looking for a way of doing like a if-statement to surround all the rules. Though I feel silly since RewriteCond get's the job done. Thanks!
kris
@kris - Yeah, that makes sense. It's typically not very easy to set something like that up when your rules are unrelated (don't depend on one another) though, since `mod_rewrite` doesn't provide the necessary control structures. Off the top of my head, you could do some hackish stuff with `RewriteCond` to condense things a bit, but that's more messy than it's worth in this case I think.
Tim Stone
Just gonna follow your example. Thanks again.
kris