我想重定向以下内容:
www.mywebsite/monkey should become www.mywebsite/chimpanseeAND
www.mywebsite/monkey/index.html should become www.mywebsite/chimpansee/index.html以下代码适用于第二种情况(使用index.html).如何在两种情况下都能正常工作?
The following code works for the second case (with index.html). How do I get it to work for both cases?
RewriteCond %{THE_REQUEST} ^GET\ /monkey/ RewriteRule ^monkey/(.*) /chimpansee/$1 [L,R=301]推荐答案
此规则应涵盖该问题-
RewriteRule ^monkey(/.*)?$ /chimpansee$1 [R=301,L]请注意,默认情况下,重定向也会执行此类操作,例如
Note that Redirect also does this kind of thing by default, e.g.
Redirect 301 /monkey/ /chimpansee/并避免使用mod_rewrite,根据该指令的位置(即conf或htaccess),可能更可取.
and that avoids using mod_rewrite, which might be preferable depending where this directive is (i.e. conf or htaccess).
要将其限制为仅GET请求(如您的代码),可以使用REQUEST_METHOD:
To limit this to only GET requests, like your code, you could use REQUEST_METHOD:
RewriteCond %{REQUEST_METHOD} ^GET$ RewriteRule ^monkey(/.*)?$ /chimpansee$1 [R=301,L]请参见 www.askapache/htaccess/mod_rewrite-variables -cheatsheet.html 以获得可在重写条件中使用的方便的环境变量列表.
See www.askapache/htaccess/mod_rewrite-variables-cheatsheet.html for a handy list of environment variables you can use in rewrite conditions.
更多推荐
htaccess重写/重定向文件夹
发布评论