如何编写htaccess通配符条件

时间:2012-05-20 19:33:33

标签: php apache .htaccess mod-rewrite

我有这个

#RewriteCond %{HTTP_HOST} ^([^\.]+)\.localhost$ [NC]
#RewriteRule ^$ /s/index.php?s=%1 [L]

在这种情况下,每当我调用test.localhost时,它都会从localhost / s / index.php获取内容?s = test

但是,当我在地址栏中输入www.localhost时,右边不应该从/s/index.php获取内容。

我如何在htaccess中写条件。

例如,如果是www =不从/s/index.php获取内容

2 个答案:

答案 0 :(得分:2)

您只需要另一个RewriteCond即可排除www.

# Matches all subdomains:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.localhost$ [NC]
# But then excludes www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^$ /s/index.php?s=%1 [L]

答案 1 :(得分:1)

所以你希望 .localhost访问localhost / s / index.php?s = 所有除了 www.localhost?最简单,最具体的方式是:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.localhost$ [NC]
RewriteCond %{HTTP_HOST} !=www.localhost [NC]
RewriteRule ^$ /s/index.php?s=%1 [L]

这使用了RewriteCond的“词法相等”检查,它应该是最准确的,比正则表达式快一点。