【问题标题】:htaccess redirects with subdomains and httpshtaccess 使用子域和 https 重定向
【发布时间】:2014-01-25 20:30:34
【问题描述】:

我正在使用托管两个域(均使用 SSL)的 HostGator 帐户。我还在这两个上都设置了不使用 SSL 的子域。我设置了重定向,以便 http 将重定向到 https,但我在处理子域时遇到问题。

这是我需要它做的事情:

http://site1.com -> https://site1.com

http://www.site1.com -> https://site1.com

dev.site1.com -> 无重定向

http://site2.com -> https://site2.com

http://www.site2.com -> https://site2.com

dev.site2.com -> 无重定向

现在发生的事情是,当我访问 dev.site2.com(无论我是否输入 https)时,我看到的是 https://site1.com,尽管它似乎没有重定向,因为地址栏仍然显示 dev.site2.com

这是我在 htaccess 中的内容:

RewriteEngine On
RewriteBase /~hostgatorusername/
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} !=dev.*
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]


# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /~hostgatorusername/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~hostgatorusername/index.php [L]
</IfModule>

# END WordPress

我仍在尝试完全掌握 htaccess,所以如果有人可以帮助我了解我做错了什么,我会很高兴。谢谢!

更新:我还需要 https://www.site1.com 重定向到 https://site1.comhttps://www.site2.com 重定向到https://site2.com

【问题讨论】:

  • !=dev.* 不是一个有效的正则表达式,除非你有一个 http 主机,它的名称中确实有一个 == 不是正则表达式中的相等测试,它只是一个像 a 或 P 或 3 这样的字符
  • 谢谢。我不太擅长正则表达式。

标签: apache .htaccess mod-rewrite ssl subdomain


【解决方案1】:

试试这个代码:

# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /~hostgatorusername/

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^dev\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~hostgatorusername/index.php [L]
</IfModule>

# END WordPress

【讨论】:

  • 这是主要的重定向,但是当我转到 dev.site2.com 时,我仍然看到 site1.com。另外,很抱歉我忘了包括这个,我需要 site1.com> 重定向到 site1.com> 和 site2.com> 重定向到 site2.com>
  • 由于某种原因,我无法在 cmets 中显示裸网址,因此我在原始帖子中进行了更新。
  • 以上规则无法为dev.site2.com 进行重定向。 1. 尝试在不同的浏览器中避免 301 缓存和 2. 检查您的 WP 永久链接设置。
  • 我尝试在完全不同的浏览器中打开它,甚至在另一台计算机上打开它。问题是,我不确定是否真的在重定向,或者是否有其他事情发生,因为当我转到 dev.site2.com 时,地址栏显示 dev.site2.com,但浏览器实际上显示的是 site1.com跨度>
  • 我需要在 site2.com 文件夹中有一个单独的 htaccess 文件吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-03
  • 2013-08-10
相关资源
最近更新 更多