.htaccess rewriterule语法

时间:2013-07-08 20:23:17

标签: apache .htaccess mod-rewrite

我需要在.htaccess中制作一些重写规则,并满足以下条件:

(www).site.com/setup   --> redirects to: www.site.com/setup
demo.site.com/setup    --> redirects to: www.site.com/demo/setup
(www).site.com/xyz   --> redirects to: www.site.com/web/xyz
demo.site.com/xyz    --> redirects to: www.site.com/demo/web/xyz

请注意,(www)表示用户既可以输入也可以不输入www,“xyz”可以是任何字符串,但不同于关键字“setup”(由于前两个规则)。

这是我尝试过的(使用[L]进行例外处理)但是我不确定它是否足够好:

RewriteCond ${REQUEST_URI} demo.
RewriteCond ${REQUEST_URI} /setup
RewriteRule /demo/setup [L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond ${REQUEST_URI} /setup
RewriteRule /setup [L]

RewriteCond ${REQUEST_URI} demo.
RewriteRule ^/(.*)$ www.site.com/demo/web/$1 [L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^/(.*)$ www.site.com/web/$1 [L]

有什么想法吗?

卢西亚诺

1 个答案:

答案 0 :(得分:0)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(demo)\.(.+)$ [NC]
RewriteRule ^(setup)/?$ http://www.%2/%1/$1 [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^(demo)\.(.+)$ [NC]
RewriteRule ^(xyz)/?$ http://www.%2/%1/web/$1 [L,R=301,NC]

RewriteCond %{HTTP_HOST} !^demo\. [NC]
RewriteRule ^(xyz)/?$ /web/$1 [L,R=301,NC]