如何使用.htaccess

时间:2019-07-08 13:35:17

标签: .htaccess url https dns mapping

我需要我所有的域都具有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上,我们才可以加密证书

我向主机提供商寻求帮助,他们告诉我将.htaccess文件添加到我的www文件夹中,但是我可能做错了事。

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是否做错了,还是我的托管支持给了我错误的信息。

谢谢

1 个答案:

答案 0 :(得分:0)

在您的.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]