apache - 带有子域和 url 模式的 mod_rewrite

标签 apache mod-rewrite apache-config

我想将子域用作 mod_rewrite 的 get 变量并使用一些参数:

例如 /page/language/site/counter -> index.php?o=operator&lg=language&s=arg1&c=arg2

如何使用 mod_rewrite 做到这一点?

RewriteEngine On  

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z]{3,6})\.example\.com  
RewriteRule ^index/([a-z]{2})/([0-9]{1,2})/([0-9]{1,2})$  http://www.example.com/index.php?o=%1&lg=%2&site=%3&counter=%4 [NC,L] # for index.php
RewriteRule ^export/([a-z]{2})/([0-9]{1,2})/([0-9]{1,2})$  http://www.example.com/export.php?o=%1&lg=%2&site=%3&counter=%4 [NC,L] # for export.php

有什么想法吗?

提前致谢

最佳答案

RewriteCond 只属于紧随其后的 RewriteRule。因此,在您的情况下,两个 RewriteCond 指令都只属于第一个 RewriteRule 指令,第二个 RewriteRule 没有关联的 RewriteCond 指令。

您可以在示例中通过合并两个 RewriteRule 来解决此问题:

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z]{3,6})\.example\.com$
RewriteRule ^(index|export)/([a-z]{2})/([0-9]{1,2})/([0-9]{1,2})$ http://www.example.com/$1.php?o=%1&lg=$2&site=$3&counter=$4 [NC,L]

另请注意 $n 而非 %n 的用法,因为 $n 指的是第 n 组 RewriteRule while %n 指的是匹配上次成功的第n组RewriteCond

关于apache - 带有子域和 url 模式的 mod_rewrite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2229385/

相关文章:

apache - 如何确定http响应头的大小?

linux - 基于 Web 的软件分发

apache - DNS 和 Web 应用程序部署

linux - 将 [a-z]* 重定向在 httpd.conf 中工作

ruby-on-rails - 如何通过 cPanel 在共享主机上部署 Ruby on Rails 应用程序?

php - HTAccess 在一个目录下重写 PHP GET

apache - .htaccess 仅将 root ("/") 重定向到子文件夹

apache - mod_rewrite : how to redirect everything except 2 files to "/"?

.htaccess - 只需使用 .htaccess 删除一个 url 段

PHP/Ratchet websockets - 无法监听 "tcp://0.0.0.0:443"