设置->服务器->使用 SSL)-6ren">
gpt4 book ai didi

apache - OpenCart 3.0.2.0 - 强制 SSL 并修复 "www".htaccess

转载 作者:太空宇宙 更新时间:2023-11-03 14:03:13 24 4
gpt4 key购买 nike

我正在尝试在 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

关于apache - OpenCart 3.0.2.0 - 强制 SSL 并修复 "www".htaccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47736091/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com