重写条件从REQUEST_URI比较顶级目录 [英] Rewrite Condition to compare top level directory from Request_URI

查看:176
本文介绍了重写条件从REQUEST_URI比较顶级目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的网站极其相似的网站设置为每一个客户多个客户。对于这些客户帐户中的文件夹都设置了组织的目的为:

I have several clients on my website with largely similar sites setup for each client. The folders for these client accounts are set up for organizational purposes at:

mydomain.com/client/sampleclient
mydomain.com/client/anotherclient

等。

我希望他们能够尽管访问他们的内容以简单/ sampleclient或/ anotherclient。要做到这一点,我已经添加了此规则,以我的根的.htaccess(感谢保罗·斯特凡 ):

I want them to be able to access their content at simply /sampleclient or /anotherclient though. To accomplish this, I've added this rule to my root .htaccess (Thanks to Paolo Stefan):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/client/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /client/$1 [L,QSA]

这在重定向的根目录下效果很好。 IE浏览器,mydomain.com/sampleclient完美重定向到mydomain.com/client/sampleclient。然而,它不为客户端目录中请求工作。是这样的:

This works well in redirecting the root directory. IE, mydomain.com/sampleclient redirects perfectly to mydomain.com/client/sampleclient. However, it doesn't work for requests within the client directories. Something like:

mydomain.com/sampleclient/orders

检查是否客户端/ sampleclient /订单是一个目录,它不是(我使用单独的URL中的客户端目录中重写),因此该规则不适用。有没有办法申请一个普通的前pression到REQUEST_URI或东西,所以我只检查是否应用该规则之前存在的REQUEST_URI的顶级目录?

Checks to see if client/sampleclient/orders is a directory, which it isn't (I'm using separate url rewriting within the client directories), so the rule is not applied. Is there a way to apply a regular expression to the REQUEST_URI or something so that I ONLY check whether the top-level directory of the request_uri exists before applying the rule?

推荐答案

的RewriteCond 能有比赛的第一个参数的一部分,所以你可以使用

RewriteCond can have a part of the match as its first argument, so you can use

RewriteCond %{DOCUMENT_ROOT}/client/$1 -d
RewriteRule ^(.+?)/(.*)$ /client/$1/$2 [L,QSA]

Apache会检查是否%{REQUEST_URI}开始( ^ )非零长度后跟斜杠的目录名,那么它会使用第一个匹配,如果看到存在该名称的目录witih %{DOCUMENT_ROOT} /客户/ 。如果是这样,重写规则将适用于任何如下斜杠(甚至没有)。

Apache will check if %{REQUEST_URI} starts (^) with a directory name of nonzero length followed by a slash, then it will use the first match to see if a directory of that name exists witih %{DOCUMENT_ROOT}/client/. If so, the rewrite rule will apply whatever follows the slash (even nothing).

例如。如果用户请求的页面

E.g. if the user requests the page

mydomain.com/sampleclient/orders

Apache会检查%{DOCUMENT_ROOT} /客户/ sampleclient 存在且是一个目录,如果是的话,它会加载

Apache will check if %{DOCUMENT_ROOT}/client/sampleclient exists and is a directory, and if so, it will load

mydomain.com/client/sampleclient/orders

通常情况下,它会加载的 mydomain.com/client/sampleclient/ [一切以第一斜线后] 的。

如果你不需要它加载 mydomain.com/client/sampleclient/orders 和希望应用在 sampleclient重写规则目录,使用

If you don't need it to load mydomain.com/client/sampleclient/orders and want to apply the rewrite rules inside the sampleclient directory, use

RewriteRule ^(.+?)/(.*)$ /client/$1/ [QSA]

也就是说,删除 $ 2 从替换字符串,而标记方括号内。

That is, remove the $2 from the replace string, and the L flag inside square brackets.

参见这些Apache的文档获取更多信息。

这篇关于重写条件从REQUEST_URI比较顶级目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆