不带斜杠的URL重定向到.htaccess中带有尾部斜杠的其他域

时间:2014-10-27 07:52:35

标签: apache .htaccess mod-rewrite redirect url-rewriting

我想将不以尾部斜杠结尾的网址重定向到不同域上具有相同路径但以尾部斜杠结尾的网址。

例如:

 origin.example.com\my-awesome-post  -> example.com\my-awesome-post\  - redirect
 origin.example.com\my-awesome-post\ - shouldn't be redirected

我当前的.htaccess配置不起作用:

<IfModule mod_rewrite.c>
 RewriteCond %{REQUEST_URI} /+[^\.]+$
 RewriteCond %{HTTP_HOST} origin.example.com\.com$ [NC]
 RewriteRule ^(.+[^/])$ http://example.com/%{REQUEST_URI}/ [R=301,L]
</IfModule>

任何建议如何使用.htaccess实现这一目标?

1 个答案:

答案 0 :(得分:1)

您的RewriteCond错误的正则表达式模式,请使用此规则:

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_URI} ^/[^.]+$
  RewriteCond %{HTTP_HOST} =origin.example.com
  RewriteRule [^/]$ http://example.com%{REQUEST_URI}/ [R=301,L]
</IfModule>
相关问题