如何在测试和实时系统中使我的两个htaccess文件相同?

时间:2017-04-06 12:18:18

标签: apache .htaccess mod-rewrite

对于我的网站,我有一个测试目录和一个实时目录。在内部我都有一个.htaccess文件略有不同,我想尽可能使它们相同。两者之间的区别在于测试系统在实时系统执行时不使用https,并且它们有2个不同的URL。

.htaccess TEST

RewriteRule ^(.*)index.html$ http://www.TEST_example.org/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^TEST_example\.org [NC]
RewriteRule ^(.*) http://www.TEST_example.org/$1 [R=301,L]

#further down...
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

.htaccess LIVE

RewriteRule ^(.*)index.html$ http://www.LIVE_example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^TEST_example\.org [NC]
RewriteRule ^(.*) http://www.LIVE_example.com/$1 [R=301,L]

#further down...

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]  

EDIT 我尝试了来自@Hello Fishy的解决方案,但我得到了

Server error!

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

If you think this is a server error, please contact the webmaster.

Error 500

server.config(TEST网站)

target_server http://www.TEST_example.org
https_switch off

.htaccess已修改(测试网站)

RewriteMap servers txt:server.config

RewriteRule ^(.*)index.html${servers:target_server}/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^TEST_example\.org [NC]
RewriteRule ^(.*) ${servers:target_server}/$1 [R=301,L]

#further down...
RewriteCond %{HTTPS} ${servers:https_switch}
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]  

1 个答案:

答案 0 :(得分:0)

RewriteMap是你的答案。我会在您的.htaccess所在的文件夹中放置一个servers.txt(或者您想要的任何内容)(但它可以放在另一个文件夹中)。它应该包含在您的TEST系统中:

target_server http://www.TEST_example.org
https_switch off

并且相应地,在您的LIVE系统上

target_server http://www.LIVE_example.com
https_switch on

您的重写必须包含servers.txt,如下所示:

RewriteMap servers txt:conf/servers.txt

/conf作为潜在的.htaccess文件夹,将servers作为别名,您将在rewrites-conf中引用。 然后重写在两个系统上都是相同的:

RewriteEngine On
RewriteMap servers txt:conf/servers.txt

RewriteRule ^(.*)index.html$ ${servers:target_server}/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^TEST_example\.org [NC]
RewriteRule ^(.*) ${servers:target_server}/$1 [R=301,L]

#further down...
#RewriteCond %{HTTPS} ${servers:https_switch}
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]