php - 将长 URL 重定向到 https

标签 php apache .htaccess ssl https

解决我的 issue 之后为了避免对 URL 的特定部分进行重定向,我现在正在尝试解决将较长、更复杂的 URL 重定向到 https 的问题

这是我的 .htaccess 文件中给我问题的行:

RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]

index.php 永远不会暴露在 URL 本身中。例如这个 URL 应该重定向到 https:

http://bootstrapcovers.com/themes/bootstrap-themes/toolsmith-1110/volvox-responsive-html5-bootstrap-template

输入 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/

相关文章:

html - 检测平板电脑和手机、htaccess 或 CSS

php - .htaccess 无法在实时系统上运行

PHP + MSSQL 使用命名参数不是 '?' 的 PDO

php - Codeigniter - 如何将数据库结果垂直显示为列而不是行

regex - 在 mod_rewrite 规则正则表达式中匹配问号

Apache mod_headers : can't edit set-cookie header

php - max_executing 时间的设置?

php - Yii2 使用存储在数组中的值填充更新表单

php - 如何将字符串变量从我的 android 应用程序发布到 php 脚本

apache - nginx 中级联index.php "try_files"