Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess重定向“;domain.tld/";至;domain.tld/home.html“;_.htaccess_Redirect_Apache2_Root - Fatal编程技术网

.htaccess重定向“;domain.tld/";至;domain.tld/home.html“;

.htaccess重定向“;domain.tld/";至;domain.tld/home.html“;,.htaccess,redirect,apache2,root,.htaccess,Redirect,Apache2,Root,我在本地主机上有一个网站: http://localhost/mysite/www/index.php 我有一些规则需要重定向,如下所示: http://localhost/mysite/www/index.php?page=home -> http://localhost/mysite/www/home.html http://localhost/mysite/www/ -> http://localhost/mysite/www/home.html 现在,我想做如下重定

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

http://localhost/mysite/www/index.php
我有一些规则需要重定向,如下所示:

http://localhost/mysite/www/index.php?page=home
->  http://localhost/mysite/www/home.html
http://localhost/mysite/www/
->  http://localhost/mysite/www/home.html
现在,我想做如下重定向:

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\u BASE
的环境变量,其中包含
/mysite/www/
。所以我想做的是比较
{REQUEST\u URI}
%{ENV:REWRITE\u 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');

谢谢您的帮助。

您想做的事情行不通,因为
mod_rewrite
不会扩展其测试模式中存在的任何变量。所以,当你这样做的时候

RewriteCond %{REQUEST_URI} =%{ENV:REWRITE_BASE}
…您实际要做的是将
%{REQUEST\u URI}
的值与字符串
“%{ENV:REWRITE\u BASE}”
进行比较,而不是像您所希望的那样将值
“/mysite/www/”
进行比较。您是否有理由不能直接在
RewriteCond
中指定值,或者简单地移动规则,使其与
/mysite/www/
目录相关


可能有另一种方法来解决这个问题,但不幸的是(出于某些好的原因),您无法使用
mod_rewrite
执行这种比较,因为
mod_rewrite
不会扩展其测试模式中存在的任何变量。所以,当你这样做的时候

RewriteCond %{REQUEST_URI} =%{ENV:REWRITE_BASE}
…您实际要做的是将
%{REQUEST\u URI}
的值与字符串
“%{ENV:REWRITE\u BASE}”
进行比较,而不是像您所希望的那样将值
“/mysite/www/”
进行比较。您是否有理由不能直接在
RewriteCond
中指定值,或者简单地移动规则,使其与
/mysite/www/
目录相关


可能还有另一种方法来解决这个问题,但不幸的是(出于某些好的原因),您无法使用
mod_rewrite
来执行这种比较,好吧。。。因此,由于我无法使用变量进行比较,因此我采用了以下方法:

# 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]

现在没事了。谢谢你的回答!;)

好的。。。因此,由于我无法使用变量进行比较,因此我采用了以下方法:

# 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]

现在没事了。谢谢你的回答!;)

呃,这个方法还有一个问题。我不能有一个像“正在建设的网站”那样的index.html。我试图在第一条重写规则之前添加一个“RewriteCond%{REQUEST_URI}index.html!-f”。。。没用。知道吗?呃,这个方法我还有一个问题。我不能有一个像“正在建设的网站”那样的index.html。我试图在第一条重写规则之前添加一个“RewriteCond%{REQUEST_URI}index.html!-f”。。。没用。有什么想法吗?