htaccess重写/重定向文件夹 [英] htaccess rewrite/redirect folder
问题描述
我想重定向以下内容:
http://www.mywebsite.com/monkey
should become
http://www.mywebsite.com/chimpansee
AND
http://www.mywebsite.com/monkey/index.html
should become
http://www.mywebsite.com/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]
请参见 http://www.askapache.com/htaccess/mod_rewrite-variables -cheatsheet.html 以获得可在重写条件中使用的方便的环境变量列表.
See http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html for a handy list of environment variables you can use in rewrite conditions.
这篇关于htaccess重写/重定向文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!