【问题标题】:How do I redirect a dynamic subdomain also based on query string?如何也基于查询字符串重定向动态子域?
【发布时间】:2011-11-20 23:07:47
【问题描述】:

我的网站将所有 *.domain.com 调用重定向到 domain.com。 我想要实现的是,首先当用户输入动态子域名时,他应该被定向到其主页,例如 如果用户写division1.domain.com,那么站点应该指向页面division.php?value=division1,当用户访问division1.domain.com/news/newsdetails.php时,这应该调用页面news.php,参数value=division1。同样,如果我将新闻页面称为基本 url,例如 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]

【问题讨论】:

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


    【解决方案1】:

    添加:

    # 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 文件。

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      相关资源
      最近更新 更多