.htaccess 重定向“domain.tld/”到“domain.tld/home.html”

发布于 2024-09-08 16:56:05 字数 903 浏览 6 评论 0原文

我的本地主机上有一个网站:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

维持三分热 2024-09-15 16:56:05

好吧...所以,由于我无法使用变量进行比较,因此我的工作方式如下:

# Redirect domain.tld/ to domain.tld/home.html
RewriteCond %{REQUEST_URI} =/www/ [OR]
RewriteCond %{REQUEST_URI} =/mysite/www/
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]

# RewriteBase equivalent - Production environment
RewriteRule . - [E=REWRITE_BASE:/www/]

# RewriteBase equivalent - Development environment
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITE_BASE:/mysite/www/,E=DEVELOPMENT_ENV:1]

# Website rewritings
RewriteRule ^([^/]*)(?:/([^/]*))?\.html$ %{ENV:REWRITE_BASE}index\.php?page=$1&view=$2 [QSA,L]

现在可以了。感谢您的回答! ;)

Okay... so, as I can't use a variable for my comparison, here is the way that I made it works :

# Redirect domain.tld/ to domain.tld/home.html
RewriteCond %{REQUEST_URI} =/www/ [OR]
RewriteCond %{REQUEST_URI} =/mysite/www/
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]

# RewriteBase equivalent - Production environment
RewriteRule . - [E=REWRITE_BASE:/www/]

# RewriteBase equivalent - Development environment
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITE_BASE:/mysite/www/,E=DEVELOPMENT_ENV:1]

# Website rewritings
RewriteRule ^([^/]*)(?:/([^/]*))?\.html$ %{ENV:REWRITE_BASE}index\.php?page=$1&view=$2 [QSA,L]

Now it's alright. Thanks for your answers! ;)

可遇━不可求 2024-09-15 16:56:05

您想要做的事情不会起作用,因为 mod_rewrite 不会扩展其测试模式中存在的任何变量。因此,当您执行这样的操作时...

RewriteCond %{REQUEST_URI} =%{ENV:REWRITE_BASE}

...您实际上所做的是将 %{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...

RewriteCond %{REQUEST_URI} =%{ENV:REWRITE_BASE}

...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 your RewriteCond 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文