apache.conf中多个网站的重写规则相同

时间:2017-03-27 16:24:00

标签: mod-rewrite virtualhost apache-config

我尝试在Apache.conf中设置多个网站以包含相同的RewriteRules,并避免在每个网站的根目录中使用.htaccess个文件。这些网站都运行相同的应用程序,.htaccess文件是相同的。这篇文章有点长,所以请耐心等待。

将规则分别应用于每个目录 WORKS

<Directory /var/www/sites/sitename/>
# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss)$ $1/ [L]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.|\?)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]

# Rewrite any calls to /render to the image parser
RewriteCond %{REQUEST_URI} render/
RewriteRule ^render/. app/parsers/slir/ [L]

# Rewrite routes to index.php if they are non-existent files/dirs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?/$1/ [L,QSA]

# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]

# Allow basic authentication
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</Directory>

我有20多个网站需要相同的规则,因此在每个目录中包含上面的整个块似乎非常无效。我尝试过一些事情,虽然我觉得自己已经做了些什么,却产生了错误的结果:

<DirectoryMatch /var/www/sites/[^/]+/>
{{ RULES_BE_HERE }}
</DirectoryMatch>
<Directory /var/www/sites/.*/>
RewriteBase /
{{ RULES_BE_HERE }}
</DirectoryMatch>

我能想出的最佳解决方案是为每个目录使用Apache包含文件,如下所示:

<Directory /var/www/sites/site1/>
  Include /etc/apache2/myrules.conf
</Directory>
<Directory /var/www/sites/site2/>
  Include /etc/apache2/myrules.conf
</Directory>
<Directory /var/www/sites/site3/>
  Include /etc/apache2/myrules.conf
</Directory>

显然,我没有太多使用apache.conf和重写规则的经验。有人有什么好的建议吗?任何帮助表示赞赏。

0 个答案:

没有答案