简体   繁体   English

OpenCart 3.0.2.0 - 强制 SSL 并修复“www”.htaccess

[英]OpenCart 3.0.2.0 - force SSL and fix “www” .htaccess

I am working on a fresh install that runs on OpenCart 3.0.2.0.我正在处理在 OpenCart 3.0.2.0 上运行的全新安装。

I bought a SSL certificate and my host activated it.我买了一个 SSL 证书,我的主机激活了它。 I then proceeded to activate SSL from OpenCart's admin (System->Settings->Server->Use SSL) and, after editing the config file, it works as intended by OpenCart (only on the pages that have forms).然后我继续从 OpenCart 的管理员(系统->设置->服务器->使用 SSL)激活 SSL,在编辑配置文件后,它按 OpenCart 的预期工作(仅在具有表单的页面上)。

However, I want the ceritificate to be visible on all pages.但是,我希望证书在所有页面上都可见。

Besides, OpenCart has some problems when typing the URL of the website with "www" before the domain name that causes font-awesome icons to not load.此外,OpenCart 在域名前输入带有“www”的网站 URL 时会出现一些问题,导致字体很棒的图标无法加载。 I could edit the config.php file and add the "www" in there but that would mess up the URL without the "www".我可以编辑 config.php 文件并在其中添加“www”,但这会弄乱没有“www”的 URL。

Figured out that the only way to force SSL on all pages AND fix the "www" problem as well is to edit my .htaccess file.发现在所有页面上强制使用 SSL 并修复“www”问题的唯一方法是编辑我的 .htaccess 文件。

I tried adding this to force SSL on all pages with no results:我尝试添加它以在所有页面上强制使用 SSL,但没有结果:

RewriteCond %{HTTP_HOST} ^uneltescu\.ro [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://uneltescu.ro/$1 [R,L]

Looked over the internet and SO and found multiple variants of forcing SSL and the "www" version to work properly but none of them seemed to work for me, maybe I didn't understood what they were aimed to achieve correctly.查看互联网和 SO,发现强制 SSL 和“www”版本正常工作的多种变体,但它们似乎都不适合我,也许我不明白它们的目标是正确实现什么。

This is how my default .htaccess file looks:这是我默认的 .htaccess 文件的样子:

# 1.To use URL Alias you need to be running apache with mod_rewrite enabled.

# 2. In your opencart directory rename htaccess.txt to .htaccess.

# For any support issues please visit: http://www.opencart.com

Options +FollowSymlinks

# Prevent Directoy listing
Options -Indexes

# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|.twig|\.ini|\.log|(?<!robots)\.txt))">
 Require all denied
## For apache 2.2 and older, replace "Require all denied" with these two lines :
# Order deny,allow
# Deny from all
</FilesMatch>

# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/storage/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

### Additional Settings that may need to be enabled for some servers
### Uncomment the commands by removing the # sign in front of it.
### If you get an "Internal Server Error 500" after enabling any of the following settings, restore the # as this means your host doesn't allow that.

# 1. If your cart only allows you to add one item at a time, it is possible register_globals is on. This may work to disable it:
# php_flag register_globals off

# 2. If your cart has magic quotes enabled, This may work to disable it:
# php_flag magic_quotes_gpc Off

# 3. Set max upload file size. Most hosts will limit this and not allow it to be overridden but you can try
# php_value upload_max_filesize 999M

# 4. set max post size. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value post_max_size 999M

# 5. set max time script can take. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_execution_time 200

# 6. set max time for input to be recieved. Uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_input_time 200

# 7. disable open_basedir limitations
# php_admin_value open_basedir none

How should a complete, working .htaccess look like for OC 3.0.2.0 to force SSL and fix the "www" problem? OC 3.0.2.0 的完整、有效的 .htaccess 应该如何强制 SSL 并修复“www”问题?

Would be nice if the code would not include my website's URL and generally work for any OC 3.0.2.0 installation so that it can further be used by the community without any further tweaking.如果代码不包含我网站的 URL 并且通常适用于任何 OC 3.0.2.0 安装,这样社区就可以进一步使用它而无需进一步调整,那就太好了。

I managed to fix it.我设法解决了它。 Here's how I did it:这是我如何做到的:

Right under the comment after RewriteEngine On I added the following code:RewriteEngine On之后的评论下,我添加了以下代码:

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

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This seems to redirect both the www and non www version to https://domain-name.com这似乎将 www 和非 www 版本都重定向到https://domain-name.com


If you want to redirect the non-www to the www version then you need to replace this line:如果要将非 www 重定向到 www 版本,则需要替换此行:

RewriteCond %{HTTP_HOST} ^www\.

With this line:有了这条线:

RewriteCond %{HTTP_HOST} !^www\.

And make sure the lines where the redirect takes place include the "www" like this:并确保发生重定向的行包含“www”,如下所示:

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

Note: Make sure to specify the www or non-www URL in your website's config files located in the public_html and admin folders according to preference.注意:确保根据偏好在位于 public_html 和 admin 文件夹中的网站配置文件中指定 www 或非 www URL。


In order to make it work for all links I had to open the config.php file located in my public_html folder and change my domain name to include https everywhere (including in the first HTTP section).为了使其适用于所有链接,我必须打开位于我的 public_html 文件夹中的 config.php 文件,并将我的域名更改为在任何地方都包含 https(包括在第一个 HTTP 部分)。

However, when I tried to access my admin page via SSL my CSS wasn't displayed.但是,当我尝试通过 SSL 访问我的管理页面时,我的 CSS 没有显示。 Fixed this by editing the config.php file that is in the admin folder in the same way.通过以相同的方式编辑 admin 文件夹中的 config.php 文件来修复此问题。 Didn't had to change it for both HTTPS and HTTP.不必为 HTTPS 和 HTTP 更改它。 I just changed it in the HTTPS section to include https://我只是在 HTTPS 部分将其更改为包含https://

Here's the .htaccess that you can copy and paste (make a backup of your own first).这是您可以复制和粘贴的 .htaccess(首先备份您自己的)。

# 1.To use URL Alias you need to be running apache with mod_rewrite enabled.

# 2. In your opencart directory rename htaccess.txt to .htaccess.

# For any support issues please visit: http://www.opencart.com

Options +FollowSymlinks

# Prevent Directoy listing
Options -Indexes

# Prevent Direct Access to files
<FilesMatch "(?i)((\.tpl|.twig|\.ini|\.log|(?<!robots)\.txt))">
 Require all denied
## For apache 2.2 and older, replace "Require all denied" with these two lines :
# Order deny,allow
# Deny from all
</FilesMatch>

# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/

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

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/storage/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

### Additional Settings that may need to be enabled for some servers
### Uncomment the commands by removing the # sign in front of it.
### If you get an "Internal Server Error 500" after enabling any of the following settings, restore the # as this means your host doesn't allow that.

# 1. If your cart only allows you to add one item at a time, it is possible register_globals is on. This may work to disable it:
# php_flag register_globals off

# 2. If your cart has magic quotes enabled, This may work to disable it:
# php_flag magic_quotes_gpc Off

# 3. Set max upload file size. Most hosts will limit this and not allow it to be overridden but you can try
# php_value upload_max_filesize 999M

# 4. set max post size. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value post_max_size 999M

# 5. set max time script can take. uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_execution_time 200

# 6. set max time for input to be recieved. Uncomment this line if you have a lot of product options or are getting errors where forms are not saving all fields
# php_value max_input_time 200

# 7. disable open_basedir limitations
# php_admin_value open_basedir none

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

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