Kristian
Kristian

Reputation: 1363

Htaccess subdomain rules

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]

Upvotes: 3

Views: 2507

Answers (1)

Tim Stone
Tim Stone

Reputation: 19169

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]

Upvotes: 2

Related Questions