静默地重写/重定向所有内容到内部子文件夹



假设我有这个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]

但是CCD_ 10消失并且CCD_。

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

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

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L,QSA]

QSA(最终)还将查询字符串附加到重写的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查询字符串Append标志

RewriteEngine On
RewriteRule ^(?!site/)(.*)$ site/$1?mode=rewrite [L,QSA]

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

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

http://www.example.com/site/index.php?mode=rewrite&one=1&two=2 
RewriteEngine On
RewriteRule ^(.*)$ site/index.php?var=$1 [L]

有了这个规则,我将所有请求传递到site/index.php,这样你就可以通过$_get['var']获得请求的uri,然后你将使index.php在场景后面提供请求的url,而不会在用户浏览器中更改url。Ciao。

最新更新