Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
Apache .htaccess-以静默方式重写/重定向所有内容到内部子文件夹_Apache_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Apache .htaccess-以静默方式重写/重定向所有内容到内部子文件夹

Apache .htaccess-以静默方式重写/重定向所有内容到内部子文件夹,apache,.htaccess,mod-rewrite,redirect,Apache,.htaccess,Mod Rewrite,Redirect,假设我有一个www.example.com网站结构: /srv/http/ /srv/http/site/index.php /srv/http/site/stuff.php 我希望发生以下重写/重定向: www.example.com/index.php->重定向到->www.example.com/site/index.php->但用户看到->www.example.com/index.php www.example.com/stuff.php->重定向到->www.example.com

假设我有一个
www.example.com
网站结构:

/srv/http/
/srv/http/site/index.php
/srv/http/site/stuff.php
我希望发生以下重写/重定向:

www.example.com/index.php
->重定向到->
www.example.com/site/index.php
->但用户看到->
www.example.com/index.php

www.example.com/stuff.php
->重定向到->
www.example.com/site/stuff.php
->但用户看到->
www.example.com/stuff.php

通常,
www.example.com/
之后的所有内容都会重定向到
www.example.com/site/
。但是用户可以在浏览器中看到原始URL

我在互联网上四处寻找,但还没有找到在这种特殊情况下应该使用什么

我试着重写一切:

RewriteEngine On
RewriteRule ^$ /site [L]
但是
index.php
将消失,并向用户显示
www.example.com/site/


如何使用
.htaccess
解决此问题?

您需要捕获传入服务器的url请求,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L,QSA]
QSA
最终也会将查询字符串附加到重写的url

RewriteEngine On

RewriteRule ^(.*)$ site/index.php?var=$1 [L]

根据这个规则,我将所有请求传递给site/index.php,这样您就可以通过$_get['var']获得请求的uri,然后让index.php在后台提供请求的url,而不会在用户浏览器中更改url。再见。

与@guido建议的想法相同,但使用负前瞻功能会稍微缩短

RewriteEngine On
RewriteRule ^(?!site/)(.*)$ site/$1 [L]

注意:我没有使用QSA标志,因为我们没有向替换URL的查询字符串添加额外参数。默认情况下,Apache将传递原始查询字符串和替换URL

http://www.example.com/index.php?one=1&two=2 
将在内部重写为

http://www.example.com/site/index.php?one=1&two=2
如果确实希望在每次重写的查询字符串中添加特殊参数(例如:
mode=rewrite
),则可以使用
QSA
查询字符串附加标志

然后将
mode=rewrite
与原始查询字符串组合

http://www.example.com/index.php?one=1&two=2 


你为什么不把DocumentRoot移到…/站点呢?下面的答案对你有用吗?这很奇怪。如果未使用[R]标志,则不应重定向浏览器,因此不应向用户显示
www.example.com/site/
。我很想在你重写之前会有其他的事情发生,比如
errordocument404
规则-因为/index.php和/stuff.php在这个位置上并不存在。嗯,更正我之前的评论:如果你没有使用[R]标志,但是你的重写url返回一个完整的url,浏览器也将被重定向。此重写规则也导致内部服务器错误。虽然
RewriteRule^foo$/bar/$1[L,QSA]
没有重写条件,但在我的情况下,这不起作用,额外的查询参数不会自动发送,因此我必须添加QSA以获得正常的get参数。我猜这取决于服务器配置(在我的例子中是XAMPP),这个解决方案是要求请求者代理在新地址重新加载资源(外部重定向),还是直接在新地址返回资源(内部重定向)?Web上的许多重定向解决方案都不包含此信息,这一点很重要,不仅因为它会影响浏览器地址栏和历史记录中显示的地址,还因为某些代理不是浏览器,不会用第二个请求响应重定向头。
http://www.example.com/index.php?one=1&two=2 
http://www.example.com/site/index.php?mode=rewrite&one=1&two=2