.htaccess 重定向“domain.tld/”到“domain.tld/home.html”
我的本地主机上有一个网站:
http://localhost/mysite/www/index.php
我有一些 RewriteRules 可以像这样重定向:
http://localhost/mysite/www/index.php?page=home
-> http://localhost/mysite/www/home.html
现在,我想做这样的重定向:
http://localhost/mysite/www/
-> http://localhost/mysite/www/home.html
我有一个名为 REWRITE_BASE
的环境变量,其中包含 /mysite /www/
。所以我想做的是将 {REQUEST_URI}
与 %{ENV:REWRITE_BASE}
进行比较......就像这样:
RewriteCond {REQUEST_URI} =%{ENV:REWRITE_BASE}
RewriteRule . %{ENV:REWRITE_BASE}home\.html [R=301,L]
但效果不佳。
为了帮助您理解我想要做什么,这里是 PHP 中的工作代码来完成我想要做的事情:
$rewriteBase = getenv('REWRITE_BASE');
if ($rewriteBase === $_SERVER['REQUEST_URI'])
header('Location: '.$rewriteBase.'home.html');
感谢您的帮助。
I have a website here on my localhost :
http://localhost/mysite/www/index.php
I have some RewriteRules to redirect like this :
http://localhost/mysite/www/index.php?page=home
-> http://localhost/mysite/www/home.html
And now, I want to do a redirection like this :
http://localhost/mysite/www/
-> http://localhost/mysite/www/home.html
I have an environment variable named REWRITE_BASE
containing /mysite/www/
. So what I thought to do was to compare {REQUEST_URI}
to %{ENV:REWRITE_BASE}
... like this:
RewriteCond {REQUEST_URI} =%{ENV:REWRITE_BASE}
RewriteRule . %{ENV:REWRITE_BASE}home\.html [R=301,L]
But it don't works well.
To help you understand what I want to do, here is the working code in PHP to do what I want:
$rewriteBase = getenv('REWRITE_BASE');
if ($rewriteBase === $_SERVER['REQUEST_URI'])
header('Location: '.$rewriteBase.'home.html');
Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧...所以,由于我无法使用变量进行比较,因此我的工作方式如下:
现在可以了。感谢您的回答! ;)
Okay... so, as I can't use a variable for my comparison, here is the way that I made it works :
Now it's alright. Thanks for your answers! ;)
您想要做的事情不会起作用,因为
mod_rewrite
不会扩展其测试模式中存在的任何变量。因此,当您执行这样的操作时......您实际上所做的是将
%{REQUEST_URI}
的值与字符串"%{ENV:REWRITE_BASE}" 进行比较
,而不是您想要的值"/mysite/www/"
。是否有原因导致您无法直接在RewriteCond
中指定值,或者只是移动规则,以便它们相对于/mysite/www/
目录开始于?可能还有另一种方法来解决该问题,但不幸的是(出于某些充分的原因)您无法使用
mod_rewrite
执行这种比较。What you want to do won't work, because
mod_rewrite
doesn't expand any variables present in its test patterns. Therefore, when you do something like this......What you're actually doing is comparing the value of
%{REQUEST_URI}
to the string"%{ENV:REWRITE_BASE}"
, instead of the value"/mysite/www/"
like you wanted. Is there a reason that you can't specify the value directly in yourRewriteCond
or simply move the rules so that they're relative to the/mysite/www/
directory to begin with?There might be another way to approach the problem, but unfortunately (and for some good reasons) you cannot perform that kind of comparison using
mod_rewrite
.