如何基于查询字符串重定向动态子域?

时间:2011-09-20 10:15:31

标签: php regex .htaccess mod-rewrite url-rewriting

我的网站将所有* .domain.com电话重定向到domain.com。 我想要实现的是,首先当用户输入动态子域名时,他应该被定向到它的主页 如果用户写division1.domain.com,那么该网站应指向页面division.php?value=division1,当用户访问division1.domain.com/news/newsdetails.php时,这应该使用参数news.php调用页面value=division1 。同样,如果我将新闻页面称为基本网址,如domain.com/news/newsdetails.php,那么这不应包含任何参数。

这是当前的htaccess代码

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com/(news/newsdetails.\.php)$  [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^([^/.]*)(.*)$ news.php?div=%1$1&filter=$2 [NC,QSA,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

RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^$ divisions.php?username=%1 [R,L]

1 个答案:

答案 0 :(得分:2)

添加:

# redirect ******XX.domain.com 
# to       domain.com/*******.php?value=******XX
# where XX is a number...
  RewriteCond %{REQUEST_URI} !news/newsdetails\.php$           # not news page
  RewriteCond %{HTTP_HOST}!^www\.domain\.com                   # prevent rewrite www
  RewriteCond %{HTTP_HOST} ^(([^.]+)[0-9]+)\.domain\.com$      # catch subdomain
  RewriteRule .* http://domain\.com/%2.php?value=%1 [R=301, L] # Redirect

# redirect ******.domain.com/news/newsdetails.php
# to       domain.com/news.php?value=******
  RewriteCond %{REQUEST_URI} news/newsdetails\.php$ # news page
  RewriteCond %{HTTP_HOST}!^www\.domain\.com        # prevent rewrite www
  RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$   # catch subdomain
  RewriteRule .* http://domain\.com/news.php?value=%1 [R=301, L] # Redirect

# redirect domain.com/news/newsdetails.php
# to domain.com/news.php
  RewriteCond %{REQUEST_URI} news/newsdetails\.php$ # news page
  RewriteRule .* /news.php?value=%1 [L]             # Redirect

到您的htaccess文件。

相关问题