将长网址重定向到https

时间:2017-02-24 21:07:05

标签: php apache .htaccess ssl https

解析我的issue以避免重定向网址的特定部分后,我现在正试图解决将长而复杂的网址重定向到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

输入网址后,它会重定向到:

[https://bootstrapcovers.com/index.php?uri=themes/bootstrap-themes/toolsmith-1110/volvox-responsive-html5-bootstrap-template][2]

请注意,我希望添加 index.php?uri = 但页面重定向回主页。如果删除该部分(index.php?uri =),则网址有效!

如何在不添加 index.php?uri = 的情况下重定向网址?

这是我完整的.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]

非常感谢任何解决此问题的帮助。

谢谢, -Paul

1 个答案:

答案 0 :(得分:1)

您需要对规则进行重新排序和重构。这样做:

# 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]

确保在测试此更改之前清除浏览器缓存。