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

时间:2011-09-21 10:51:30

标签: php .htaccess dynamic url-rewriting subdomain

我在.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]

1 个答案:

答案 0 :(得分:11)

试试这个:

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]