根据标题,当我手动输入网站 URL(主题末尾的示例)时,域名末尾的尾部斜杠会被自动删除。我已强制 SSL 重定向,并启用了 Apache ModRewrite.htaccess这是文件内容(如您所见,它是由 Prestashop CMS(1.7.x)自动生成的):
# ~~start~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again
# .htaccess automaticaly generated by PrestaShop e-commerce open-source solution
# http://www.prestashop.com - http://www.prestashop.com/forums
<IfModule mod_rewrite.c>
<IfModule mod_env.c>
SetEnv HTTP_MOD_REWRITE On
</IfModule>
RewriteEngine on
#Domain: www.example.com
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api(?:/(.*))?$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
# Images
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$1$2$3$4.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$1$2$3$4$5.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]
# AlphaImageLoader for IE and fancybox
RewriteRule ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 [L]
# Dispatcher
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ %{ENV:REWRITEBASE}index.php [NC,L]
</IfModule>
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType application/font-woff .woff
AddType font/woff2 .woff2
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|svg)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
#If rewrite mod isn't enabled
ErrorDocument 404 /index.php?controller=404
# ~~end~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again
例子:
- www.example.com/contacts -> https://www.example.comcontacts [WRONG]
- example.com/contacts -> https://www.example.comcontacts [WRONG]
- http://example.com/contacts -> https://www.example.comcontacts [WRONG]
- https://example.com/contacts -> https://www.example.com/contacts [OK]
- https://www.example.com/contacts -> https://www.example.com/contacts [OK]
任何帮助都将受到赞赏。
答案1
正如评论中提到的,您发布的配置中没有任何内容会导致这种情况。您发布的配置中根本没有重定向。这很可能是您的 CMS(Prestashop)中的配置错误。
问题似乎出在 HTTP 到 HTTPS 重定向,它删除了 URL 路径开头(主机名之后)的斜杠。
但是,您可以通过实施 HTTP 到 HTTPS(以及非 www 到 www)重定向来“解决”此问题.htaccess
- 此操作在您的 CMS 能够执行任何操作之前发生。因此,您的 CMS 不需要重定向请求。
例如,假设您有一个相当标准的服务器,其中 SSL 由应用程序服务器管理(没有像 Cloudflare 等的 SSL 代理),那么您可以在文件的最顶部.htaccess
、注释之前执行如下操作# ~~start~~
。
# Redirect non-www to www + HTTPS
RewriteCond %{HTTP_HOST} ^(example\.com) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
# Redirect HTTP to HTTPS (remaining requests)
RewriteCond %{HTTPS} =off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
注意:首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。
更新:
我尝试添加您上面写的几行但没有任何改变。
这意味着服务器配置本身中的 HTTP 到 HTTPS 格式不正确(在之前执行.htaccess
)
<VirtualHost *:80> ServerName www.example.com Redirect permanent / https://www.example.com </VirtualHost>
如果这是您的实际配置,则您在目标 URL 上缺少尾部斜杠!这将导致您看到的格式错误(域后缺少斜杠)重定向。
它应该是:
Redirect permanent / https://www.example.com/
您需要确保在测试之前清除浏览器缓存,因为错误的重定向将被缓存。