mod_rewrite with subdomain and url pattern

坚强是说给别人听的谎言 提交于 2019-12-10 18:15:34

问题


I want to use the subdomain as a get variable with mod_rewrite AND use some parameter :

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

How to do that with 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

Any idea?

Thanks in advance


回答1:


A RewriteCond does only belong to the directly following RewriteRule. So in your case both RewriteCond directives do only belong to the first RewriteRule directive and the second RewriteRule has no associated RewriteCond directives.

You can fix this in your example by merging the two RewriteRules:

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]

Also note the usage of $n instead of %n as $n refers to the match of the n-th group of RewriteRule the while %n refers to the match of the n-th group of the last successful RewriteCond.



来源:https://stackoverflow.com/questions/2229385/mod-rewrite-with-subdomain-and-url-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!