Mod Rewrite Directory Checking

馋奶兔 提交于 2019-12-24 10:28:44

问题


I have a situation where I have several clients, each of which I'd like to be able to access their site via mydomain.com/clientname. To keep things organized, I store the actual files for the client's sites in /client/clientname. I can achieve the desired effect by putting lots and lots of these lines in my .htaccess:

RewriteRule ^client1(.*)$ /client/client1$1 [L]
RewriteRule ^client2(.*)$ /client/client2$1 [L]

etc.

I'm trying to do this in a cleaner way, by checking if the client directory exists in /client or not. (If I've determined that it's not an otherwise valid file or directory). But for some reason this seems to not be working:

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

What am I missing?


回答1:


From the Apache mod_rewrite docs:

REQUEST_FILENAME

The full local filesystem path to the file or script matching the request, if this has already been determined by the server at the time REQUEST_FILENAME is referenced. Otherwise, such as when used in virtual host context, the same value as REQUEST_URI.

Your line:

RewriteCond /client/%{REQUEST_FILENAME} -d

seems to cause the problem, because it makes Apache look for the following file path:

/client/[the whole filesystem path of requested file]

which is quite sure not what you want.

You may correct it as follows:

RewriteCond %{DOCUMENT_ROOT}/client%{REQUEST_URI} -d


来源:https://stackoverflow.com/questions/6112387/mod-rewrite-directory-checking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!