.htaccess 将子文件夹重定向到 HTTPS
我正在尝试为我的 .htaccess 文件编写 RewriteRule。具体来说,每当用户访问特定子目录时,我希望它重写以强制 HTTPS 连接。
例如,每当有人访问:http://www.mydomain.com/subdirectory
(以及该“子目录”的任何其他子目录)。
我希望它重写到 https://www.mydomain.com/subdirectory
我已经尝试了以下操作,但它似乎创建了一个循环:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
此外,此 .htaccess 文件放置在我的域的根。
关于如何修改我的 RewriteRule 有什么想法吗?
非常感谢!
I'm trying to write a RewriteRule for my .htaccess file. Specifically, whenever a user accesses a specific subdirectory, I would like it to Rewrite to force an HTTPS connection.
For example, whenever someone accesses: http://www.mydomain.com/subdirectory
(and any other sub-directories of that "subdirectory").
I'd like it to rewrite to https://www.mydomain.com/subdirectory
I've tried the following, but it appears to create a loop:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
Also, this .htaccess file is placed in the root of my domain.
Any ideas on how to modify my RewriteRule?
Many Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我会将其放入域的根目录中:
I would put this into the domain's root directory:
这对我有用,这允许您重定向到 https 特定文件夹,只需在文件夹内添加一个包含以下内容的 htaccess 文件:
This work for me, this allow you to redirect to https a specific folder, just add an htaccess file inside of the folder with the following content:
啊,这是两者的结合。 MonoMano - 您在 RewriteRule 的第一部分中省略了子目录,因此将所有流量定向到 HTTPS 子域地址。我发现检查 'off' 比检查 !=on 更可靠,不要问我为什么!
您需要按照 Floern 的回复添加子目录,请参见下文:
Ahh, its a combo of the two. MonoMano - you've omitted the subdirectory in the first part of the RewriteRule, therefore directing ALL traffic to the HTTPS subdomain address. I found that checking for 'off' was more reliable than checking for !=on, dont ask me why!
You'd want to add the subdirectory back in as per Floern's response, see below:
将下面给出的行添加到该子目录的 .htaccess 文件中:
Add the lines given below to .htaccess file of that subdirectory:
如果您不希望地址栏显示子文件夹,请使用此选项。它将把
yourdomain.com
重定向到yourdomain.com/subfolder
,但看起来您仍在yourdomain.com
Use this if you don't want the address bar to show the subfolder. It will redirect
yourdomain.com
toyourdomain.com/subfolder
but will look like you are still onyourdomain.com
试试这个
Try This
这将删除
www
前缀,并强制使用https
:为主域更改
example.com
,为子域更改subdirectory
目录This will remove
www
prefix , and forcehttps
:Change
example.com
for you main domain andsubdirectory
for Sub Directory尝试
Try