.htaccess - 静默重写/重定向到内部子文件夹

时间:2022-09-17 07:56:30

Let's say I have thiswww.example.com site structure:

假设我有这个www.example.com站点结构:

/srv/http/
/srv/http/site/index.php
/srv/http/site/stuff.php

I want the following rewrites/redirects to happen:

我希望以下重写/重定向发生:

www.example.com/index.php -> redirects to -> www.example.com/site/index.php -> but the user sees -> www.example.com/index.php

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

www.example.com/stuff.php -> redirects to -> www.example.com/site/stuff.php -> but the user sees -> www.example.com/stuff.php

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

In general, everything after www.example.com/ redirects to www.example.com/site/. But the user sees the original URL in the browser.

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

I've looked around on the internet but haven't managed to figure out what to use in this particular situation.

我在互联网上环顾四周,但没有设法弄清楚在这种特殊情况下使用什么。

I tried rewriting everything:

我尝试重写一切:

RewriteEngine On
RewriteRule ^$ /site [L]

but index.php disappears and www.example.com/site/ is shown to the user.

但index.php消失,并向用户显示www.example.com/site/。

How can I use .htaccess to solve this problem?

我如何使用.htaccess来解决这个问题?

3 个解决方案

#1


7  

You need to capture the url request incoming into the server, like this:

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

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

The QSA is (eventually) to also append the query string to the rewritten url

QSA(最终)也将查询字符串附加到重写的URL

#2


5  

Same idea as @guido suggested, but a bit shortened using negative lookahead

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

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

Note: I am not using QSA flag as we are not adding additional parameters to the query string for the replacement URL. By default, Apache will pass the original query string along with the replacement URL.

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

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

will be internally rewritten as

将在内部重写为

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

If you really want add a special parameter (ex: mode=rewrite) in the query string for every rewrite, then you can use the QSA Query String Append flag

如果你真的想在查询字符串中为每次重写添加一个特殊参数(例如:mode = rewrite),那么你可以使用QSA查询字符串追加标志

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

Then this will combine mode=rewrite with original query string

然后,这将将mode = rewrite与原始查询字符串组合在一起

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

to

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

#3


1  

RewriteEngine On

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

With this rule, i'm passing all requests to site/index.php, so you could get the requested uri via $_GET['var'], and then you'll make the index.php serve the requested url behind the scene without the url changing in the user's browser. Ciao.

使用此规则,我将所有请求传递给site / index.php,因此您可以通过$ _GET ['var']获取请求的uri,然后您将使index.php服务于场景后面的请求的URL没有在用户的浏览器中更改网址。再见。

#1


7  

You need to capture the url request incoming into the server, like this:

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

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

The QSA is (eventually) to also append the query string to the rewritten url

QSA(最终)也将查询字符串附加到重写的URL

#2


5  

Same idea as @guido suggested, but a bit shortened using negative lookahead

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

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

Note: I am not using QSA flag as we are not adding additional parameters to the query string for the replacement URL. By default, Apache will pass the original query string along with the replacement URL.

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

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

will be internally rewritten as

将在内部重写为

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

If you really want add a special parameter (ex: mode=rewrite) in the query string for every rewrite, then you can use the QSA Query String Append flag

如果你真的想在查询字符串中为每次重写添加一个特殊参数(例如:mode = rewrite),那么你可以使用QSA查询字符串追加标志

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

Then this will combine mode=rewrite with original query string

然后,这将将mode = rewrite与原始查询字符串组合在一起

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

to

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

#3


1  

RewriteEngine On

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

With this rule, i'm passing all requests to site/index.php, so you could get the requested uri via $_GET['var'], and then you'll make the index.php serve the requested url behind the scene without the url changing in the user's browser. Ciao.

使用此规则,我将所有请求传递给site / index.php,因此您可以通过$ _GET ['var']获取请求的uri,然后您将使index.php服务于场景后面的请求的URL没有在用户的浏览器中更改网址。再见。