更改为https并重定向到.htaccess中的子文件夹规则 [英] change to https and redirect to subfolder rule in .htaccess
问题描述
我想在这里做两件事:
-
重定向到子文件夹
redirect to a sub-folder
重定向 http://www.something.com/some/page.html 或者 https://www.something.com/some/page.html 到 https://www.something.com/subfolder/some/page.html
redirect http://www.something.com/some/page.html or https://www.something.com/some/page.html to https://www.something.com/subfolder/some/page.html
将http重定向到https
redirect http to https
重定向 http://www.something.com/subfolder/some/page.html 到 https://www.something.com/subfolder/some/page.html
我想将它们都放在同一个.htaccess文件中
And I want to do both of them in the same .htaccess file
我已经能够通过以下代码重定向到子文件夹:
I have been able to redirect to subfolder by the following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} something.com [NC]
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]
然后我要同时做这两个;即将http转换为https(仅在有http请求的情况下),并通过以下代码重定向到子文件夹:
And then I am trying to do both of them; i.e. http to https(only if http request comes) and redirect to subfolder by the following code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} something.com [NC]
RewriteRule ^(.*)$ https://www.something.com/subfolder/$1 [R=301,NC]
但是它不起作用. 我在这里做什么错了?
But it's not working. What am I doing wrong here?
编辑
使用@starkeen解决方案时;即
When using @starkeen's solution; i.e.
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ https://www.example.com/subfolder/$1 [R=301,NC,L]
我期望得到以下结果:
当我提供以下任何内容时:
when I give any of the following:
http://example.com/brands/omega.html OK
https://example.com/brands/omega.html OK
http://www.example.com/brands/omega.html OK
https://www.example.com/brands/omega.html OK
http://example.com/subfolder/brands/omega.html WRONG
http://www.example.com/subfolder/brands/omega.html WRONG
但是最后两个要重定向到
But the last two are redirecting to
推荐答案
以下是在一条规则中同时完成两项任务的一条规则:
Here is a rule to do both the tasks in a single rule:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !^/subfolder [NC]
RewriteRule ^(?:subfolder)?(/.*)?$ https://www.example.com/subfolder$1 [NE,L,R=302,NC]
在测试此规则之前,请确保清除浏览器缓存.
Make sure to clear your browser cache before testing this rule.
这篇关于更改为https并重定向到.htaccess中的子文件夹规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!