apache - Htaccess 将 http 或 https 从 domain.com 重定向到 https domain.net

标签 apache .htaccess ssl redirect url-rewriting

我在同一个网站上有两个域,一个是 .com 带 ssl,另一个是 .net 不带 ssl
我们只想使用 .com 扩展名,并使用 htaccess 文件将我们的用户从旧域重定向到新域(我尝试使用此代码):

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.[^.]+\.[^.]+$
RewriteRule ^(.*) http://www\.%{HTTP_HOST}/$1       [QSA,L,R=301]

Rewritecond %{REMOTE_HOST}  \.exemple\.(net) [nc]
Rewriterule ^(.*)$ https://www.exemple.com [r=301,nc]

RewriteRule ^([^\.]+)$ $1.php [NC,L]

ErrorDocument 404 https://www.exemple.com/404

这显然行不通!

exemple.net  ------>   https://www.exemple.com/404 

如有任何帮助,我们将不胜感激。

最佳答案

这是修改后的版本,修复了您的建议中的一些问题。

主要的变化是你需要在第二个重写规则中重用capure $1:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^/?(.*)$ https://www.example.com/$1 [QSA,R=301]

Rewritecond %{HTTP_HOST}  \.example\.net$
Rewriterule ^/?(.*)$ https://www.example.com/$1 [QSA,R=301]

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI}.php -f
RewriteRule ^/?([^\.]+)$ $1.php [NC,L]

ErrorDocument 404 https://www.example.com/404

请注意,我自己并没有对此进行测试,我只是通过查看来应用了一些修复。

该规则集在 http 服务器的主机配置或动态配置文件 (".htaccess") 中同样有效。您绝对应该更喜欢第一个选项,除非您无权访问配置(阅读:便宜的托管服务提供商)。

显然需要加载和启用重写模块,如果您决定使用动态配置文件,请注意它位于主机的 DOCUMENT_ROOT 文件夹中并且可由 http 读取服务器进程。

关于apache - Htaccess 将 http 或 https 从 domain.com 重定向到 https domain.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54439929/

相关文章:

php - Linux Ubuntu 14.00 - 找不到 Apache2 文件错误 - 文件存在的地方

apache - 为什么Apache指向/var/www?

.htaccess - StackOverflow 中的 301 重定向。它是如何运作的?

node.js - 为什么 apache 重写 URL 两次?

apache - Laravel 总是返回 404

apache - 使用 ConcurrentUpdateSolrServer 需要哪些 jar?

php - 重定向多个 slug htaccess

google-app-engine - AppEngine SNI https 和谷歌网站管理员工具

spring-boot - 带有客户端证书的 RestTemplate

asp.net-mvc - 在 ASP.NET MVC 1.0 中在 HTTP 和 HTTPS 之间切换