解决我的 issue 之后为了避免对 URL 的特定部分进行重定向,我现在正在尝试解决将较长、更复杂的 URL 重定向到 https 的问题
这是我的 .htaccess 文件中给我问题的行:
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
index.php 永远不会暴露在 URL 本身中。例如这个 URL 应该重定向到 https:
输入 URL 后重定向到:
[https://bootstrapcovers.com/index.php?uri=themes/bootstrap-themes/toolsmith-1110/volvox-responsive-html5-bootstrap-template][2]
请注意,添加了 index.php?uri=,这是我所期望的,但该页面已重定向回主页。如果我删除该部分 (index.php?uri=),则 url 有效!
如何在不添加 index.php?uri= 的情况下重定向 URL?
这是我完整的 .htaccess 文件:
# 1 Month for most static assets
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico|woff)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
Options -MultiViews
RewriteEngine on
RewriteRule ^(.+?)/$ /$1 [R=302,NE,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+preview [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+preview [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
如果您能帮助解决这个问题,我们将不胜感激。
谢谢, -保罗
最佳答案
您需要对规则进行重新排序和一些重构。就这样吧:
# 1 Month for most static assets
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico|woff)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+preview [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+preview [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
确保在测试此更改之前清除浏览器缓存。
关于php - 将长 URL 重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42448278/