【问题标题】:mod_rewrite: url rewriting plus subdomain (wildcard) rewrite at the same timemod_rewrite:url重写加子域(通配符)同时重写
【发布时间】:2011-01-11 03:30:43
【问题描述】:

我的.htaccess 文件中有两条用于url 重写的规则:

  1. 子域重写:xxx.domain.com 内部重定向到file.php?item=xxx

    RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
    
    RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$ [NC]
    
    RewriteRule ^$ /file.php?item=%2 [QSA,nc]
    
  2. 普通重写:

    RewriteRule ^([A-Za-z0-9_)(:!-',]+)/?$ file.php?item=$1 [L] 
    

我需要实现的是编写第三条规则,将这两条规则结合起来而不与它们冲突。即,在这条线的下方(或上方)我需要有类似的东西:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com/([A-Za-z0-9_)(:!-',]+)$ [NC]

RewriteRule ^$ /anotherfile.php?item1=%2&item2=$1 [QSA,nc]

这样http://xxx.domain.com/yyy 将被重定向到 anotherfile.php?item1=xxx&item2=yyy

任何可行的想法,或者正确的方法是什么?

【问题讨论】:

  • 为了将来参考,这类问题更适合 ServerFault (serverfault.com)。由 StackOverflow 团队运行,专供服务器和系统管理员使用。

标签: .htaccess mod-rewrite subdomain wildcard


【解决方案1】:

试试这个规则:

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com$ [NC]
RewriteRule ^([A-Za-z0-9_)(:!-',]+)/?$ /file.php?item1=%2&item2=$1 [QSA]

它使用第一个规则的条件和第二个规则的 URL 路径模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-09
    • 2014-06-09
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2014-02-28
    相关资源
    最近更新 更多