【问题标题】:Removing query_string from wildcard subdomain rewrite从通配符子域重写中删除 query_string
【发布时间】:2011-11-16 05:15:26
【问题描述】:

我的目标是指出:

http://xyz.domain.com/abchttp://www.domain.com/dir/file.php?var=xyz

(换句话说,任何带有http://_____.domain.com/abc 的内容都将从http://www.domain.com/dir/file.php?var=_____ 读取。)

我在网站根目录 (http://www.domain.com/.htaccess) 的 .htaccess 文件中使用了它:

RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule ^abc/*$ dir/file.php?var=%2 [NC,L]

它起作用了,但是当我只想要http://xyz.domain.com/abc 而没有显示查询字符串时,它会自动重定向到http://xyz.domain.com/abc/?var=xyz

我考虑在上面添加 %{QUERY_STRING} 行,但在尝试将两个 RewriteCond 的 vars 相互匹配时卡住了。

然后我在http://www.askapache.com/htaccess/modrewrite-tips-tricks.html#Removing_Query_String找到了这个

RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.askapache.com%{REQUEST_URI}? [R=301,L]

...我猜这是在常规域上摆脱此类内容的正确代码?我尝试通过将其更改为以下内容使其适用于通配符子域:

RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com[NC]
RewriteRule .* http://%2.domain.com%{REQUEST_URI}? [R=301,L]

但它不起作用。所以...有人可以帮忙吗?

【问题讨论】:

  • 您的文档根目录中是否有“abc”目录?

标签: .htaccess query-string subdomain wildcard


【解决方案1】:

有一个名为 mod_dir 的模块,当它认为您正在尝试访问一个目录但缺少尾部斜杠时,它会重定向浏览器。当我在我的 apache 上进行测试时,它只会在目录实际存在时发生,我会使用尾部斜杠重定向,但会发生重写并且我看到查询字符串。尝试在内部强制使用尾部斜杠,并在重写到 dir/file 时,明确使用尾部斜杠:

# Force trailing slash when accessing /abc without one
RewriteRule ^abc$ /abc/ [NC,L]

# these are the same
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
# Match against a trailing slash
RewriteRule ^abc/$ dir/file.php?var=%2 [NC,L]

【讨论】:

  • 谢谢,成功了!抱歉,我猜它一个真正的目录。在我的脑海中,我在想我可以为abc 的任何单词重复这个,不管是不是目录。我现在明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 2014-06-09
  • 1970-01-01
  • 2011-01-11
  • 2023-04-02
  • 2014-06-11
  • 1970-01-01
相关资源
最近更新 更多