htaccess重写具有不同路径的文件夹

时间:2013-07-16 23:52:39

标签: .htaccess mod-rewrite

我在本地和登台服务器上设置了Web应用程序。本地应用程序具有与登台不同的基本路径。 e.g。

本地 http://phils-imac.local/git/clients/myproject/html/

分段 http://myserver.com/myproject/html/

我想使用htaccess来访问没有'html'部分的网址。例如 http://phils-imac.local/git/clients/myproject/ http://myserver.com/myproject/

我一直在我的登台服务器上使用此重写规则:

RewriteEngine On
RewriteCond %{SERVER_NAME} =myserver.com
RewriteCond %{REQUEST_URI} !^/myproject/html/.*$
RewriteRule ^(.*)$ /myproject/html/$1 [L]

它工作正常,但我觉得我需要为每个项目自定义它。理想情况下,我希望'myproject'部分是一个变量,并且规则更通用,因此它也适用于我的本地路径。

1 个答案:

答案 0 :(得分:0)

您无法在.htaccess中定义变量,但如果您保持项目名称相同,则可以在重写规则中容纳变量基本路径

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !/html/ [NC]
RewriteRule ^(.*?)/?myproject/(.*)$ $1/myproject/html/$2 [NC,L]

这需要您的网络根目录中的.htaccess。如果.htaccess位于myproject文件夹中,请尝试以下操作:

RewriteEngine On

RewriteCond %{REQUEST_URI} !/html/ [NC]
RewriteRule ^(.*)$ html/$1 [L]

在这两种情况下,您都需要放弃%{SERVER_NAME}支票,因为它在您的本地和登台位置之间永远不会匹配。