簡體   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