RewriteBase 如何在 .htaccess 中工作 [英] How does RewriteBase work in .htaccess
问题描述
我在几个 .htaccess
示例中看到了这一点
I have seen this in a few .htaccess
examples
RewriteBase /
它在功能上似乎与 HTML 的 <base href="">
有点相似.
It appears to be somewhat similar in functionality to the <base href="">
of HTML.
我相信它可能会自动将其值添加到 RewriteRule
语句(可能没有前导斜线的语句)的开头?
I believe it may automatically prepend its value to the beginning of RewriteRule
statements (possibly ones without a leading slash)?
我无法让它正常工作.我认为它的使用对于站点可移植性非常有用,因为我经常有一个与生产服务器不同的开发服务器.我当前的方法让我删除了 RewriteRule
语句中的部分.
I could not get it to work properly. I think it's use could come in very handy for site portability, as I often have a development server which is different to a production one. My current method leaves me deleting portions out of my RewriteRule
statements.
谁能向我简要解释一下如何实现它?
Can anyone explain to me briefly how to implement it?
谢谢
推荐答案
用我自己的话来说,在阅读文档并进行实验后:
In my own words, after reading the docs and experimenting:
您可以使用 RewriteBase
为您的重写提供 base.考虑这个
You can use RewriteBase
to provide a base for your rewrites. Consider this
# invoke rewrite engine
RewriteEngine On
RewriteBase /~new/
# add trailing slash if missing
rewriteRule ^(([a-z0-9-]+/)*[a-z0-9-]+)$ $1/ [NC,R=301,L]
这是我用来确保 URL 具有尾部斜杠的真实规则.这将转换
This is a real rule I used to ensure that URLs have a trailing slash. This will convert
http://www.example.com/~new/page
到
http://www.example.com/~new/page/
通过将 RewriteBase
放在那里,您可以使相对路径脱离 RewriteBase
参数.
By having the RewriteBase
there, you make the relative path come off the RewriteBase
parameter.
这篇关于RewriteBase 如何在 .htaccess 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!