.htaccess重定向只有URL包含末尾的数字和标记

时间:2011-08-10 10:34:29

标签: apache .htaccess redirect

我想将每个包含数字的网址重定向到子域名:

http://example.com/773 to http://blog.example.com/773

http://example.com/482/comment... to http://blog.example.com/482/comment...

和重定向网址包含“tag”,例如:

http://example.com/tag/free-psd to http://blog.example.com/tag/free-psd

http://example.com/tag/jquery to http://blog.example.com/tag/jquery

而非重定向网址包含如下字符串:

http://example.com/web or http://example.com/web/shop or http://example.com/about

2 个答案:

答案 0 :(得分:1)

使用正则表达式构造RewriteRules以仅匹配数字URL:

    RewriteEngine On
    RewriteRule ^/([0-9]+(/.+)?) http://blog.example.com/$1

同样对于标记网址:

    RewriteRule ^/(tag/.+) http://blog.example.com/$1

答案 1 :(得分:1)

RewriteCond %{REQUEST_URI}   ^\/(\d+)$
RewriteRule   (.*)  http://blog.example.com/%1

RewriteCond %{REQUEST_URI}   ^\/(\d+)\/comment$
RewriteRule   (.*)  http://blog.example.com/%1/comment

RewriteCond %{REQUEST_URI}   ^\/tag\/(.+)$
RewriteRule   (.*)  http://blog.example.com/tag/%1