Apache Generic.htaccess用于存储在子目录中的多个网站

Apache Generic.htaccess用于存储在子目录中的多个网站,apache,.htaccess,mod-rewrite,host,Apache,.htaccess,Mod Rewrite,Host,我的开发环境设置为使用单个主机(localhost)。我正在我的机器上开发多个网站,每个网站都存储在自己的目录下,如下所示: /var/www/site1 /var/www/site2 ... http://localhost/site1/home/red -> http://localhost/site1/index.php?page=home&p1=red http://localhost/site2/index/param1/param2/pa

我的开发环境设置为使用单个主机(
localhost
)。我正在我的机器上开发多个网站,每个网站都存储在自己的目录下,如下所示:

     /var/www/site1
     /var/www/site2
     ...
http://localhost/site1/home/red -> http://localhost/site1/index.php?page=home&p1=red
http://localhost/site2/index/param1/param2/param3 -> http://localhost/site2/index.php?page=index&p1=param1&p2=param2&p3=param3
RewriteEngine On
RewriteBase /site1/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?page=$1&p1=$2&p2=$3&p4=$4 [L,QSA]
在我的计算机上,文档根目录设置为
/var/www

我正在对这些网站中的大多数网站使用URL重写,大多数
。htaccess
文件将以不同的方式重写子目录,以获取参数,如下所示:

     /var/www/site1
     /var/www/site2
     ...
http://localhost/site1/home/red -> http://localhost/site1/index.php?page=home&p1=red
http://localhost/site2/index/param1/param2/param3 -> http://localhost/site2/index.php?page=index&p1=param1&p2=param2&p3=param3
RewriteEngine On
RewriteBase /site1/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?page=$1&p1=$2&p2=$3&p4=$4 [L,QSA]
我也倾向于将这些网站复制到不同的目录下,当我这样做时,我必须对我复制的网站的
.htaccess
文件进行大量更改

我想知道是否有办法定义包含网站根目录(而不是主机文档根目录)的常量,以及如何将其与重写规则一起使用,以便在复制网站时只需更改一行代码(将此常量设置为不同的值)

换一种形式来说,是否有一种方法可以执行与网站根目录而不是主机/
%{HTTP_host}
(即网站的“主机”是
localhost/site1
而不是
localhost
)相关的重写,以及如何做到这一点

我已尝试在脚本开始时从每个请求中删除主机,并在脚本结束时将其重新添加,但这不适用于使用
[L]
选项的重写规则

谢谢大家!

问候,,
Lucian

您可以使用如下规则创建htaccess文件:

     /var/www/site1
     /var/www/site2
     ...
http://localhost/site1/home/red -> http://localhost/site1/index.php?page=home&p1=red
http://localhost/site2/index/param1/param2/param3 -> http://localhost/site2/index.php?page=index&p1=param1&p2=param2&p3=param3
RewriteEngine On
RewriteBase /site1/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?page=$1&p1=$2&p2=$3&p4=$4 [L,QSA]

然后将其放入目录
/var/www/site1
,如果您想将其应用于site2,请更改
重写库
,并将规则放入
/var/www/site2

,您可以使用如下规则创建htaccess文件:

     /var/www/site1
     /var/www/site2
     ...
http://localhost/site1/home/red -> http://localhost/site1/index.php?page=home&p1=red
http://localhost/site2/index/param1/param2/param3 -> http://localhost/site2/index.php?page=index&p1=param1&p2=param2&p3=param3
RewriteEngine On
RewriteBase /site1/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?page=$1&p1=$2&p2=$3&p4=$4 [L,QSA]
并将其放入目录
/var/www/site1
,如果要将其应用于site2,请更改
重写库
,并将规则放入
/var/www/site2