.htaccess 子域参数在查询字符串中不可访问

发布于 2024-12-06 06:12:14 字数 1784 浏览 1 评论 0原文

我在 .htaccess 中编写了以下规则,

RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com 
RewriteCond %1 !^www$ [NC]
RewriteCond %{REQUEST_URI} ^/news/news-details\.php$
RewriteRule (.*) news.php?div=%1

它的作用是将请求传输到 news.php 文件,其中 div 变量等于 news/news-details.php。我不需要这样的 div 。我需要使用域写入的子域的值,就像用户来自 user.domain.com/news/news-details.php 一样。我需要 news.php 文件中的 div 变量为用户,即 news.php?div=user

编辑: 现在是 Htaccess 代码它导致根新闻详细信息页面出现问题选项

+FollowSymLinks
RewriteEngine on

# For Accessing News Details Page for root http://www.domain.com/news/news-details.php
RewriteCond %{HTTP_HOST} ^www.domain\.com [NC]
RewriteRule ^news/news-details.php$ news.php [QSA,NC,L]


# For Accessing News Details Page http://user.domain.com/news/news-details.php
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com [NC]
RewriteCond %{HTTP_HOST) !^www\.
RewriteRule ^news/news-details.php$  news.php?div=%1 [QSA,NC,L


# For www.domain.com it should go to the index page
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]


# For Accessing Divisions Page user.domain.com should go to domain.com/divisions.php?division=user
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^$ /divisions.php?division=%1 [L]

再次编辑:

这里这两个规则是冲突的

# For www.domain.com it should go to my-index.php page
#
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ my-index.php [NC,L]


# For Page URL http://www.domain.com/news/news-details.php
#
RewriteCond %{REQUEST_URI} ^/news/news\-details\.php [NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$  [NC]
RewriteRule ^(.*)$ my-news.php [NC,QSA,L]

I have written the following rules in .htaccess

RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com 
RewriteCond %1 !^www$ [NC]
RewriteCond %{REQUEST_URI} ^/news/news-details\.php$
RewriteRule (.*) news.php?div=%1

What it does is transfer the request the news.php file with the div variable equal to news/news-details.php. I do not need the div to be like that. I need the value of the subdomain that has been written with the domain like if the user is coming from user.domain.com/news/news-details.php. I need the div variable in the news.php file to be user i.e. news.php?div=user.

EDIT:
Here is now the Htaccess code It is causing problem for the root news details page Options

+FollowSymLinks
RewriteEngine on

# For Accessing News Details Page for root http://www.domain.com/news/news-details.php
RewriteCond %{HTTP_HOST} ^www.domain\.com [NC]
RewriteRule ^news/news-details.php$ news.php [QSA,NC,L]


# For Accessing News Details Page http://user.domain.com/news/news-details.php
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com [NC]
RewriteCond %{HTTP_HOST) !^www\.
RewriteRule ^news/news-details.php$  news.php?div=%1 [QSA,NC,L


# For www.domain.com it should go to the index page
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]


# For Accessing Divisions Page user.domain.com should go to domain.com/divisions.php?division=user
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^$ /divisions.php?division=%1 [L]

AGAIN EDIT:

Here these two rules are conflicting

# For www.domain.com it should go to my-index.php page
#
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ my-index.php [NC,L]


# For Page URL http://www.domain.com/news/news-details.php
#
RewriteCond %{REQUEST_URI} ^/news/news\-details\.php [NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$  [NC]
RewriteRule ^(.*)$ my-news.php [NC,QSA,L]

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

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

发布评论

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

评论(1

⊕婉儿 2024-12-13 06:12:14

试试这个:

RewriteCond %{REQUEST_URI} ^/news/news\-details.php$
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.domain\.com$  [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)$ news.php?div=%1 [NC,QSA,L]

RewriteCond %{REQUEST_URI} ^/news/news\-details\.php [NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$  [NC]
RewriteRule ^(.*)$ news.php [NC,QSA,L]

RewriteCond %{HTTP_HOST} ^www\.domain\.com$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [NC,QSA,L]

RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.domain\.com$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ divisions.php?division=%1 [NC,QSA,L]

Try this:

RewriteCond %{REQUEST_URI} ^/news/news\-details.php$
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.domain\.com$  [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)$ news.php?div=%1 [NC,QSA,L]

RewriteCond %{REQUEST_URI} ^/news/news\-details\.php [NC]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$  [NC]
RewriteRule ^(.*)$ news.php [NC,QSA,L]

RewriteCond %{HTTP_HOST} ^www\.domain\.com$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [NC,QSA,L]

RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.domain\.com$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ divisions.php?division=%1 [NC,QSA,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文