htaccess重写/重定向文件夹

编程入门 行业动态 更新时间:2024-10-21 06:32:11
本文介绍了htaccess重写/重定向文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想重定向以下内容:

www.mywebsite/monkey should become www.mywebsite/chimpansee

AND

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重写/重定向文件夹

本文发布于:2023-10-31 13:06:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   文件夹   重定向   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!