apache - .htaccess if/else 取决于 {SERVER_NAME}?

标签 apache .htaccess

我有这个 .htaccess 文件,但我希望它只在我在实时站点上时才这样做。

有什么办法可以:

// if server_name looks like example.com to this
// else dont run this bit
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (auth|register|secure|payment|admin|trading_careers)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(auth|register|secure|payment|admin|trading_careers)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

我试过使用 SetEnvIfNoCaseIfDefine没有成功。真的不知道是否可以做我想做的事。

先感谢您。

编辑( future 读者的解决方案示例):
// if https is off
RewriteCond %{HTTPS} off
// if server_name like example.com (case insensitive) OR
RewriteCond %{SERVER_NAME} =example.com [NC,OR]
// server_name like www.example.com (case insensitive)...
RewriteCond %{SERVER_NAME} =www.example.com [NC]
RewriteCond %{REQUEST_URI} (auth|register|secure|payment|admin|trading_careers)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

感谢您的大力帮助。

最佳答案

您可以将此条件添加到您的规则中:

RewriteCond %{SERVER_NAME} =www.example.com

关于apache - .htaccess if/else 取决于 {SERVER_NAME}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3119922/

相关文章:

apache - Tomcat 集群环境中的 JDBC 连接池

apache - 在网络上看不到网站,但在本地可以看到

php - 在 $_GET 中使用转义 & 符号的 htaccess 重写规则失败

php - 如何拥有同名的文件和目录

php - 此 mod_rewrite 规则如何导致我的任何文件无法加载?

apache - .htaccess 将 imgname.jpg 改写为 image.php?img=imgname

apache - 为什么在使用 Hadoop 处理后数据大小会发生变化?

apache - 是否可以在 apache 中记录响应的第一行?

java - Apache 服务器未启动,但在 Eclipse 中显示正在启动

php - PHP 中的动态 .htaccess 文件