htaccess重定向和重写规则冲突

时间:2014-05-09 15:37:44

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

我想重写对index.php的所有请求?r = 这样/某事就变成了/index.php?r=sth

我已应用此规则有效:

RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png|pdf|php)$ [NC]
RewriteRule ^(.+)$ index.php?r=$1 [L]

但是我想要重定向,例如

Redirect 301 /sth /sth-new

它会起作用,但网址会变为:

/sth-new?r=sth

您有任何解决方案 - 有关为何发生这种情况的建议吗? 如何实现全局规则并具有简单的重定向?

1 个答案:

答案 0 :(得分:0)

你可以这样做:

RewriteRule ^sth/?$ /sth-new? [L,R=301]

RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !\.(jpg|css|js|gif|png|pdf|php)$ [NC]
RewriteRule ^(.+)$ index.php?r=$1 [L]

您应该在内部重写规则之前保留外部重定向规则。

相关问题