Htaccess子域规则

时间:2010-08-16 12:03:26

标签: .htaccess subdomain

我使用htaccess和一套规则来控制我网站的不同页面。

  1. 如果用户访问我的主网站(domain.com),而不使用www,则301重定向到www.domain.com(用于搜索引擎优化)
  2. 我使用通配符子域,因此如果用户访问subdomain.domain.com,他们将查看决定为其提供内容的特定PHP文件。
  3. 我的问题是我想扩展我的设置,以便能够使用相同的网址名称。举个例子,如果我访问subdomain.domain.com/feed,我会看到来自主域规则的pages / public_news_feed.php文件,因为它共享了“/ feed”规则。我真正想要的是pages / public_subdomain_blog_feed.php!

    如何为domain.com和subdomain.domain.com设置特定规则?

    提前致谢并度过美好的一天!

    #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 个答案:

答案 0 :(得分:2)

好吧,您已经知道如何判断用户是否在子域中,因为您已经为控制器执行了此操作:

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

尽管可能会或者可能无法简化整个规则集,但最简单的方法是以相同的方式调整规则。由于子域规则较少,我们可以移动主站点规则以上的规则并使用您拥有的条件有条件地应用重写:

## 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]