在.htaccess中使用基于环境变量的RewriteCond

时间:2022-08-26 23:18:59

I am trying to define an environment variable in my .htaccess file, and then run a rewrite condition using that variable. Below is what I have in my .htaccess file, but the rewrite is not working:

我试图在我的.htaccess文件中定义一个环境变量,然后使用该变量运行重写条件。以下是我的.htaccess文件中的内容,但重写不起作用:

RewriteEngine on
#set to 'live' or 'maintenance'
SetEnv ENVIRONMENT maintenance

#If the ENVIRONMENT variable is 'mainetnance' then show a maintenance page to the users
RewriteCond %{ENV:ENVIRONMENT} maintenance
RewriteRule ^(.*)$ /maintenance.html

The purpose of this is to set the site to maintenance mode programmatically by having PHP edit the .htaccess file when it receives a post request from one of GitHub's hooks to pull the repo for an update.

这样做的目的是通过让PHP编辑.htaccess文件来编程地将站点设置为维护模式,当它从GitHub的一个钩子接收一个post请求以拉回repo进行更新时。

3 个解决方案

#1


12  

mod_rewrite doesn't use variables set via SetEnv, instead use this method:

mod_rewrite不使用通过SetEnv设置的变量,而是使用此方法:

#set to 'live' or 'maintenance'
RewriteRule .* - [E=STATUS:maintenance]

#If the ENVIRONMENT variable is 'maintenance' then show a maintenance page
RewriteCond %{REQUEST_URI} !maintenance.html [NC]
RewriteCond %{ENV:STATUS} ^maintenance$
RewriteRule ^.*$ /maintenance.html [L]

The first line of the bottom three, makes sure that once the user is redirected to the file "maintenance.html", it will not be redirected again. Else the user keeps getting redirected to the same file, causing an 500 internal server error saying "AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error."

底部三行的第一行确保一旦用户被重定向到文件“maintenance.html”,它就不会再被重定向。此外,用户不断被重定向到同一文件,导致500内部服务器错误,说“AH00124:由于可能的配置错误,请求超过了10个内部重定向的限制”。

#2


3  

Bit late - but for all the others searching ...

有点迟了 - 但是对于所有其他人来说......

use SetEnvIf - see: mod_rewrite rule and setenv

使用SetEnvIf - 请参阅:mod_rewrite rule和setenv

SetEnvIf Request_URI ".*" ENVIRONMENT=maintenance

SetEnvIf Request_URI“。*”ENVIRONMENT =维护

...

...

#3


3  

I found that nearly every example fell short of incomplete, so I'm bringing my solution to the table. I needed to create a variable based on my server environment - either local or production - then run rewrites based on that server environment. Specifically, if the environment is production, I want to force https and www.

我发现几乎每个例子都不完整,所以我把我的解决方案带到了桌面上。我需要根据我的服务器环境(本地或生产)创建变量,然后根据该服务器环境运行重写。具体来说,如果环境是生产,我想强制https和www。

Why not have a unique .htaccess file for each server environment? Because, I want to include .htaccess in my Git repository and not worry about screwing up one of my environments.

为什么不为每个服务器环境提供唯一的.htaccess文件?因为,我想在我的Git存储库中包含.htaccess,而不用担心搞砸我的一个环境。

The first thing you need to know is that all of this must occur within mod_rewrite, which means that you must have mod_rewrite enabled in your Apache instance. If you do not know how to check this, create a phpinfo page (http://www.phpinfofile.com/) and do a search for "mod_rewrite" in the "Loaded Modules" section.

您需要知道的第一件事是所有这些必须在mod_rewrite中发生,这意味着您必须在Apache实例中启用mod_rewrite。如果您不知道如何检查,请创建一个phpinfo页面(http://www.phpinfofile.com/)并在“已加载的模块”部分中搜索“mod_rewrite”。

The .htaccess code:

.htaccess代码:

<IfModule mod_rewrite.c>

# Set SERVER_ENV=production if HTTP_HOST matches my production URL
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule .* - [E=SERVER_ENV:production]

# Force www. if SERVER_ENV is production
RewriteCond %{ENV:SERVER_ENV} ^production$
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force https if SERVER_ENV is production
RewriteCond %{ENV:SERVER_ENV} ^production$
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Set SERVER_ENV=local if HTTP_HOST matches my local URL
RewriteCond %{HTTP_HOST} ^www-local\.mysite\.local$ [NC]
RewriteRule .* - [E=SERVER_ENV:local]

</IfModule>

Notes:

笔记:

  1. The first variable name is "SERVER_ENV" and the value is "production". This variable name/value could be anything. For example, you could use MY_ENVIRONMENT:live. After you've created this variable in .htaccess, you should be able to find it referenced in your phpinfo file - a great way to check that it's there.
  2. 第一个变量名是“SERVER_ENV”,值是“production”。这个变量名称/值可以是任何东西。例如,您可以使用MY_ENVIRONMENT:live。在.htaccess中创建此变量之后,您应该能够在phpinfo文件中找到它 - 这是检查它是否存在的好方法。

在.htaccess中使用基于环境变量的RewriteCond

  1. The RewriteCond lines should be before the RewriteRule and should be grouped together as shown. You can have 1 or more rewrite conditions that must be met before the rewrite rule kicks in.

    RewriteCond行应该在RewriteRule之前,并且应该如图所示组合在一起。在重写规则启动之前,您必须满足一个或多个重写条件。

  2. I'm setting a different variable if HTTP_HOST is "local". I don't have any rewrite conditions/rules that use the "local" variable - I'm just setting it here for the sake of instruction.

    如果HTTP_HOST是“本地”,我正在设置一个不同的变量。我没有任何使用“本地”变量的重写条件/规则 - 我只是为了教学而在这里设置它。

#1


12  

mod_rewrite doesn't use variables set via SetEnv, instead use this method:

mod_rewrite不使用通过SetEnv设置的变量,而是使用此方法:

#set to 'live' or 'maintenance'
RewriteRule .* - [E=STATUS:maintenance]

#If the ENVIRONMENT variable is 'maintenance' then show a maintenance page
RewriteCond %{REQUEST_URI} !maintenance.html [NC]
RewriteCond %{ENV:STATUS} ^maintenance$
RewriteRule ^.*$ /maintenance.html [L]

The first line of the bottom three, makes sure that once the user is redirected to the file "maintenance.html", it will not be redirected again. Else the user keeps getting redirected to the same file, causing an 500 internal server error saying "AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error."

底部三行的第一行确保一旦用户被重定向到文件“maintenance.html”,它就不会再被重定向。此外,用户不断被重定向到同一文件,导致500内部服务器错误,说“AH00124:由于可能的配置错误,请求超过了10个内部重定向的限制”。

#2


3  

Bit late - but for all the others searching ...

有点迟了 - 但是对于所有其他人来说......

use SetEnvIf - see: mod_rewrite rule and setenv

使用SetEnvIf - 请参阅:mod_rewrite rule和setenv

SetEnvIf Request_URI ".*" ENVIRONMENT=maintenance

SetEnvIf Request_URI“。*”ENVIRONMENT =维护

...

...

#3


3  

I found that nearly every example fell short of incomplete, so I'm bringing my solution to the table. I needed to create a variable based on my server environment - either local or production - then run rewrites based on that server environment. Specifically, if the environment is production, I want to force https and www.

我发现几乎每个例子都不完整,所以我把我的解决方案带到了桌面上。我需要根据我的服务器环境(本地或生产)创建变量,然后根据该服务器环境运行重写。具体来说,如果环境是生产,我想强制https和www。

Why not have a unique .htaccess file for each server environment? Because, I want to include .htaccess in my Git repository and not worry about screwing up one of my environments.

为什么不为每个服务器环境提供唯一的.htaccess文件?因为,我想在我的Git存储库中包含.htaccess,而不用担心搞砸我的一个环境。

The first thing you need to know is that all of this must occur within mod_rewrite, which means that you must have mod_rewrite enabled in your Apache instance. If you do not know how to check this, create a phpinfo page (http://www.phpinfofile.com/) and do a search for "mod_rewrite" in the "Loaded Modules" section.

您需要知道的第一件事是所有这些必须在mod_rewrite中发生,这意味着您必须在Apache实例中启用mod_rewrite。如果您不知道如何检查,请创建一个phpinfo页面(http://www.phpinfofile.com/)并在“已加载的模块”部分中搜索“mod_rewrite”。

The .htaccess code:

.htaccess代码:

<IfModule mod_rewrite.c>

# Set SERVER_ENV=production if HTTP_HOST matches my production URL
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule .* - [E=SERVER_ENV:production]

# Force www. if SERVER_ENV is production
RewriteCond %{ENV:SERVER_ENV} ^production$
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force https if SERVER_ENV is production
RewriteCond %{ENV:SERVER_ENV} ^production$
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Set SERVER_ENV=local if HTTP_HOST matches my local URL
RewriteCond %{HTTP_HOST} ^www-local\.mysite\.local$ [NC]
RewriteRule .* - [E=SERVER_ENV:local]

</IfModule>

Notes:

笔记:

  1. The first variable name is "SERVER_ENV" and the value is "production". This variable name/value could be anything. For example, you could use MY_ENVIRONMENT:live. After you've created this variable in .htaccess, you should be able to find it referenced in your phpinfo file - a great way to check that it's there.
  2. 第一个变量名是“SERVER_ENV”,值是“production”。这个变量名称/值可以是任何东西。例如,您可以使用MY_ENVIRONMENT:live。在.htaccess中创建此变量之后,您应该能够在phpinfo文件中找到它 - 这是检查它是否存在的好方法。

在.htaccess中使用基于环境变量的RewriteCond

  1. The RewriteCond lines should be before the RewriteRule and should be grouped together as shown. You can have 1 or more rewrite conditions that must be met before the rewrite rule kicks in.

    RewriteCond行应该在RewriteRule之前,并且应该如图所示组合在一起。在重写规则启动之前,您必须满足一个或多个重写条件。

  2. I'm setting a different variable if HTTP_HOST is "local". I don't have any rewrite conditions/rules that use the "local" variable - I'm just setting it here for the sake of instruction.

    如果HTTP_HOST是“本地”,我正在设置一个不同的变量。我没有任何使用“本地”变量的重写条件/规则 - 我只是为了教学而在这里设置它。

相关文章