Htaccess规则用于索引文件的动态子域冲突

时间:2011-09-21 13:45:58

标签: .htaccess url-rewriting subdomain

这是我的htaccess文件

Options +FollowSymLinks
RewriteEngine on

# For Accessing Page 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 www.domain.com it should go to the index page
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]

当我在浏览器中输入http://www.domain.com/news/news-details.php时,它会将我带到index.php页面而不是news.php。为什么?

编辑: 检查这些规则

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

2 个答案:

答案 0 :(得分:10)

Options +FollowSymLinks
RewriteEngine On

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

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

# For Accessing Users Page
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^$ /users.php?user=%1 [L]

# For www.domain.com it should go to my-index.php page
# (but only if requested resource is not real file)
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /my-index.php [L]

答案 1 :(得分:0)

您不能在路径中输入文件的名称。

RewriteRule ^news/news-details.php$  news.php [QSA,NC,L]

这里有文件“news.php”,在路径中有“新闻”。 尝试另一个词而不是路径中的新闻或文件的其他名称。

例如:

RewriteRule ^news/news-details.php$  news_main.php [QSA,NC,L]