繁体   English   中英

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

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

我正在处理在 OpenCart 3.0.2.0 上运行的全新安装。

我买了一个 SSL 证书,我的主机激活了它。 然后我继续从 OpenCart 的管理员(系统->设置->服务器->使用 SSL)激活 SSL,在编辑配置文件后,它按 OpenCart 的预期工作(仅在具有表单的页面上)。

但是,我希望证书在所有页面上都可见。

此外,OpenCart 在域名前输入带有“www”的网站 URL 时会出现一些问题,导致字体很棒的图标无法加载。 我可以编辑 config.php 文件并在其中添加“www”,但这会弄乱没有“www”的 URL。

发现在所有页面上强制使用 SSL 并修复“www”问题的唯一方法是编辑我的 .htaccess 文件。

我尝试添加它以在所有页面上强制使用 SSL,但没有结果:

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

查看互联网和 SO,发现强制 SSL 和“www”版本正常工作的多种变体,但它们似乎都不适合我,也许我不明白它们的目标是正确实现什么。

这是我默认的 .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

OC 3.0.2.0 的完整、有效的 .htaccess 应该如何强制 SSL 并修复“www”问题?

如果代码不包含我网站的 URL 并且通常适用于任何 OC 3.0.2.0 安装,这样社区就可以进一步使用它而无需进一步调整,那就太好了。

我设法解决了它。 这是我如何做到的:

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]

这似乎将 www 和非 www 版本都重定向到https://domain-name.com


如果要将非 www 重定向到 www 版本,则需要替换此行:

RewriteCond %{HTTP_HOST} ^www\.

有了这条线:

RewriteCond %{HTTP_HOST} !^www\.

并确保发生重定向的行包含“www”,如下所示:

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

注意:确保根据偏好在位于 public_html 和 admin 文件夹中的网站配置文件中指定 www 或非 www URL。


为了使其适用于所有链接,我必须打开位于我的 public_html 文件夹中的 config.php 文件,并将我的域名更改为在任何地方都包含 https(包括在第一个 HTTP 部分)。

但是,当我尝试通过 SSL 访问我的管理页面时,我的 CSS 没有显示。 通过以相同的方式编辑 admin 文件夹中的 config.php 文件来修复此问题。 不必为 HTTPS 和 HTTP 更改它。 我只是在 HTTPS 部分将其更改为包含https://

这是您可以复制和粘贴的 .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