.htaccess内部重定向无法正常工作

时间:2014-06-25 15:02:21

标签: regex apache .htaccess mod-rewrite

我尝试为内部网页执行一些.htaccess重定向,但它们对我不起作用。这是我的.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

RewriteRule articles articles/how-to-play-piano [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

我也在<IfModule>之外尝试了这个但是没有工作:

Redirect 301 http://www.domain.com/articles http://www.domain.com/articles/how-to-play-piano

Redirect 301 /articles http://www.domain.com/articles/how-to-play-piano

1 个答案:

答案 0 :(得分:1)

articles规则的正则表达式错误,会导致无限循环。要解决此问题,您需要使用锚点^ and $

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

RewriteRule ^articles/?$ articles/how-to-play-piano [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]