htaccess子域规则

发布于 2024-09-15 00:31:17 字数 1852 浏览 3 评论 0原文

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

  1. 如果用户访问我的主站点 (domain.com),而不使用 www,则 301 重定向到 www.domain.com(出于 SEO 目的)
  2. 我使用通配符子域,因此如果用户访问 subdomain.domain.com,他们将看到决定其内容的特定 PHP 文件。

我的问题是我想扩展我的设置,以便能够使用相同的网址名称。例如,如果我访问 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]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-09-22 00:31:17

好吧,您已经知道如何确定用户是否位于子域上,因为您已经为控制器执行了此操作:

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]

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文