htaccess 中域名特定的代码块

发布于 2024-09-30 06:23:30 字数 526 浏览 0 评论 0原文

我们有本地服务器、中央开发服务器、临时服务器和生产服务器。然而,由于显而易见的原因,开发和登台受到密码保护。因此,在对 htaccess 进行任何更改后,我必须手动编辑 htaccess 文件以在开发和登台服务器上启用密码保护。

有没有什么方法可以根据域名设置条件块,例如:

if ( $domain == "dev.example.com" || $domain == "staging.example.com" ){
  AuthName "Password Protected Area"
  AuthType Basic
  AuthUserFile /somewhere/.htpasswd
  Require valid-user
}

我需要找到与条件等效的 htaccess:

if ( $domain == "dev.example.com" || $domain == "staging.example.com" ){

}

我将不胜感激你们可以提供的任何帮助或指示。

We have local servers, central dev, staging and production servers. However the dev and staging are password protected for obvious reasons. So after deploying any changes to the htaccess i have to manually edit the htaccess file to enable the password protection on the dev and staging server.

Is there any way to have conditional blocks based on domain name like:

if ( $domain == "dev.example.com" || $domain == "staging.example.com" ){
  AuthName "Password Protected Area"
  AuthType Basic
  AuthUserFile /somewhere/.htpasswd
  Require valid-user
}

I need to find the htaccess equivalent of the condition:

if ( $domain == "dev.example.com" || $domain == "staging.example.com" ){

}

I would appreciate the any help or pointers you guys can give.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜深人未静 2024-10-07 06:23:30

虽然通过 VirtualHost 而不是 .htaccess 来完成此操作要好得多,但如果 setenvif 模块处于活动状态,您可以使用以下解决方案:

SetEnvIf Host ^dev\.site\.com$ is_on_dev_site
SetEnvIf Host ^staging\.site\.com$ is_on_dev_site
Order deny,allow
Deny from env=is_on_dev_site
# require password if access would otherwise be denied
Satisfy any
# Put your password auth stuff here

您还可以使用白名单来完成此操作,这可能更好,因为它可以确保您的开发站点即使在如果有人决定允许通过 www.dev.site.com 等访问它们:

SetEnvIf Host ^site\.com$ is_on_public_site
SetEnvIf Host ^www\.site\.com$ is_on_public_site
Order deny,allow
Deny from all
Allow from env=is_on_public_site
Satisfy any
# Put your password auth stuff here

如果您的服务器上没有 mod_setenvif,mod_rewrite 也可以为您完成这项工作(将白名单示例中的 SetEnvIf 块替换为以下内容) :

RewriteEngine On
RewriteCond %{HTTP_HOST} =site.com
RewriteRule ^ - [E=is_on_public_site:yes]
RewriteCond %{HTTP_HOST} =www.site.com
RewriteRule ^ - [E=is_on_public_site:yes]

While doing it through the VirtualHost and not a .htaccess is much better, you can use the following solution if the setenvif module is active:

SetEnvIf Host ^dev\.site\.com$ is_on_dev_site
SetEnvIf Host ^staging\.site\.com$ is_on_dev_site
Order deny,allow
Deny from env=is_on_dev_site
# require password if access would otherwise be denied
Satisfy any
# Put your password auth stuff here

You could also do it with a whitelist which is probably better as it ensures your dev sites are still protected even if someone decides to allow access to them via www.dev.site.com etc.:

SetEnvIf Host ^site\.com$ is_on_public_site
SetEnvIf Host ^www\.site\.com$ is_on_public_site
Order deny,allow
Deny from all
Allow from env=is_on_public_site
Satisfy any
# Put your password auth stuff here

If you do not have mod_setenvif on your server, mod_rewrite can also do the work for you (replace the SetEnvIf blocks in the whitelist example with the following):

RewriteEngine On
RewriteCond %{HTTP_HOST} =site.com
RewriteRule ^ - [E=is_on_public_site:yes]
RewriteCond %{HTTP_HOST} =www.site.com
RewriteRule ^ - [E=is_on_public_site:yes]
鹊巢 2024-10-07 06:23:30

我找到了一个很好的解决方案,来区分“localhost”与“live”:

由于 htaccess 的条件有些有限,为什么不选择 IfModule?:比较您拥有的模块(即使用 ,并寻找一个重要的,希望是长期的-术语差异,例如,如果您在 Windows 上开发并在 Linux 上部署 mod_win32.c 可能会很好(不要忘记添加 phpinfo() 省略的 .c。)

进行操作(经过测试):

<IfModule mod_win32.c> 
    RewriteRule ^banana$ test.php?dudeThisIsLocal=1
</IfModule>
<IfModule !mod_win32.c> 
    RewriteRule ^banana$ test.php?dudeThisIsLive=1
</IfModule>

然后您可以像这样 示例可以进行良好的完整性测试,浏览到 yourdomain/banana 或 localhost/banana,如果您(已启用重写)将 $_GET 数组转储到 test.php 中,则使用您的真实配置语句填充代码。

I found a nice solution, to distinguish "localhost" vs. "live":

Since the conditionals of htaccess are somewhat limited why not settle for IfModule?: Compare the modules you have (i.e. using , and look for a significant, hopefully long-term difference, e.g. if you develop on windows and deploy on linux mod_win32.c might be good. ( Don't forget to add the .c which phpinfo() ommits.)

Then you can go about it like this (tested):

<IfModule mod_win32.c> 
    RewriteRule ^banana$ test.php?dudeThisIsLocal=1
</IfModule>
<IfModule !mod_win32.c> 
    RewriteRule ^banana$ test.php?dudeThisIsLive=1
</IfModule>

This example makes for a good sanity test, browing to yourdomain/banana resp. localhost/banana and if you (having rewrite enabled ) dump the $_GET array in test.php. If this works, fill the codeforks with your real config statements.

掀纱窥君容 2024-10-07 06:23:30

您应该在您的站点配置文件中执行此操作:

<VirtualHost domain.com:80>

...config statements here

</VirtualHost>

并且

<VirtualHost domain2.com:80>

....config statements here

</VirtualHost>

如果您使用的主机不允许您编辑站点配置文件(如果您使用共享主机,则这种情况确实有可能发生),那么您应该考虑 VPS 或专用主机。

You should do this in your sites conf file with:

<VirtualHost domain.com:80>

...config statements here

</VirtualHost>

and

<VirtualHost domain2.com:80>

....config statements here

</VirtualHost>

If you are with a host who does not allow you to edit your sites config file, which is a real possibility if you are with a shared host then you should consider VPS or dedicated hosting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文