简体   繁体   English

使用 htaccess 在共享主机中将网站 http 重定向到 laravel 中的 https

[英]Redirect website http to https in laravel in shared hosting using htaccess

I have a Laravel project.我有一个 Laravel 项目。 and I used Godaddy.我用了Godaddy。 Recently, I installed an SSL certificate on my website so when I type https://example.com in the browser it works but when I write example.com, it connects by HTTP.最近,我在我的网站上安装了一个 SSL 证书,所以当我在浏览器中输入https://example.com时它可以工作,但是当我编写 example.com 时,它通过 HTTP 连接。

To fix that, I added these lines to my root folder's .htaccess file:为了解决这个问题,我将这些行添加到我的根文件夹的 .htaccess 文件中:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

But this redirects to me https://example.com/public .但这会重定向到我https://example.com/public

I added two htaccess one is in the root folder and the second one is in public folder.我添加了两个 htaccess 一个在根文件夹中,第二个在公共文件夹中。

first htaccess code :第一个 htaccess 代码:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L,NC]

Second htaccess code is :第二个 htaccess 代码是:

Options -MultiViews -Indexes选项 -MultiViews -Indexes

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^ %1 [L,R=301]


# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

How can I achieve this https://example.com .我怎样才能实现这个https://example.com

Thanks in advance!提前致谢!

Try this code:试试这个代码:

RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R]
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
<IfModule mod_rewrite.c>
    RewriteEngine on
    Options +FollowSymlinks
    #redirect http non-www to https://www
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(www\.)?your-domain\.com$
    RewriteRule (.*) https://www.your-domain.com/$1 [R=301,L]
    #redirect https non-www to www
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^your-domain\.com$
    RewriteRule (.*) https://www.your-domain.com/$1 [R=301,L]
</IfModule>

I am using this to redirect.我正在使用它来重定向。

Here is the Solution,这是解决方案,

<IfModule mod_rewrite.c>
   RewriteEngine On
   # Force SSL
   RewriteCond %{HTTPS} !=on
   RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
   # Remove public folder form URL
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Tried every solution but this worked for me on Laravel 6.0 , use this by creating .htaccess file in root folder rather than /public/.htaccess file.尝试了所有解决方案,但这在Laravel 6.0上对我有用,通过在根文件夹中创建 .htaccess 文件而不是 /public/.htaccess 文件来使用它。

Options +SymLinksIfOwnerMatch
RewriteEngine On

# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
RewriteRule ^ index.php [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM