.htaccess - 如何使用 .htaccess 映射不同的 URL
问题描述
我需要我的所有域都有 https 证书,但是当我创建 .htaccess 文件时,网站没有关闭。
故事是这样的:我有一个 Ruby on Rails 网站,我们称之为:web1.com.br
我也有域名:web1.com、web2.com.br 和 web2.com
我进行了 DNS 映射,因此每当人们键入这 3 个域时,他们都会转到我在 web1.com.br 上的原始主机(并且不更改用户的 url)并且它工作正常。
但是当有人键入其他 3 个域时,我没有我的 https 证书。只有在我原来的网站 web1.com.br 我有让我们加密证书
我向我的主机提供商寻求帮助,他们告诉我在我的 www 文件夹中添加一个 .htaccess 文件,但我可能做错了什么。
RewriteEngine On
RewriteCond %{HTTP_HOST} ^web1.com.br.
RewriteRule ^ http://www.web1.com.br%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^web1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.web1\.com$
RewriteRule ^$ http\:\/\/www\.web1\.com\.br\/ [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^web1.com.br.
RewriteRule ^ http://www.web1.com.br%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^web2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.web2\.com$
RewriteRule ^$ http\:\/\/www\.web1\.com\.br\/ [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^web1.com.br.
RewriteRule ^ http://www.web1.com.br%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^web2\.com\.br$ [OR]
RewriteCond %{HTTP_HOST} ^www\.web2\.com\.br$
RewriteRule ^$ http\:\/\/www\.web1\.com\.br\/ [R=301,L]
我创建了 .htaccess 文件,我的网站(在所有域上)关闭了,给出了 500 响应:
"The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at web@web1.com.br to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request."
我删除了文件,网站又重新上线了
您可能会说,我是个菜鸟,但找不到可以帮助我的人。我不知道是我做错了 .htaccess 还是我的托管支持给了我错误的信息。
谢谢
解决方案
在您的 .htaccess 文件中检查此规则,将所有内容重定向到https://web1.com.br/
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?web1\.com [NC]
RewriteRule (.*) https://web1.com.br/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?web2\.com [NC]
RewriteRule (.*) https://web1.com.br/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?web2\.com\.br [NC]
RewriteRule (.*) https://web1.com.br/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.web1\.com\.br [NC]
RewriteRule (.*) https://web1.com.br/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^web1\.com\.br [NC]
RewriteRule (.*) https://web1.com.br/$1 [R=301,L]
推荐阅读
- c++ - 有没有办法将 SDL_Surface 转换为 Raylib Texture2D?
- matlab - 如何以矢量化方式平均矩阵中的每个第 n 个元素?
- reactjs - 作为 jsFiddle 的 ReactJS 代码有语法错误
- swift - 快速语法, func(var:var:) 作为闭包?
- javascript - 解析模式微数据并像谷歌数据结构一样排列它们
- python-3.x - 如何在 python 中为这个桥带宽问题编码?
- html - 需要解释为什么将内容添加到 div 会改变位置
- php - 将字符串中的项目分成两个数组
- mysql - SQL ORDER BY 使用另一个表中的值使用按重复项分组的公式
- javascript - 没有物品时如何设置购物车状态?