.htaccess 将子文件夹重定向到 HTTPS

发布于 2024-10-13 11:11:47 字数 495 浏览 4 评论 0原文

我正在尝试为我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

喜爱纠缠 2024-10-20 11:11:47

我会将其放入域的根目录中:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/$1 [R=301,L]

I would put this into the domain's root directory:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/$1 [R=301,L]
调妓 2024-10-20 11:11:47

这对我有用,这允许您重定向到 https 特定文件夹,只需在文件夹内添加一个包含以下内容的 htaccess 文件:

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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:

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
无名指的心愿 2024-10-20 11:11:47

啊,这是两者的结合。 MonoMano - 您在 RewriteRule 的第一部分中省略了子目录,因此将所有流量定向到 HTTPS 子域地址。我发现检查 'off' 比检查 !=on 更可靠,不要问我为什么!

您需要按照 Floern 的回复添加子目录,请参见下文:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]

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:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
ぃ双果 2024-10-20 11:11:47

将下面给出的行添加到该子目录的 .htaccess 文件中:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/subdirectory/$1 [L,R=301]

Add the lines given below to .htaccess file of that subdirectory:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/subdirectory/$1 [L,R=301]
山色无中 2024-10-20 11:11:47

如果您不希望地址栏显示子文件夹,请使用此选项。它将把 yourdomain.com 重定向到 yourdomain.com/subfolder,但看起来您仍在 yourdomain.com

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$ 
RewriteRule ^(/)?$ subfolder/index.php [L]

Use this if you don't want the address bar to show the subfolder. It will redirect yourdomain.com to yourdomain.com/subfolder but will look like you are still on yourdomain.com

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$ 
RewriteRule ^(/)?$ subfolder/index.php [L]
带上头具痛哭 2024-10-20 11:11:47

试试这个

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

Try This

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
要走就滚别墨迹 2024-10-20 11:11:47

这将删除 www 前缀,并强制使用 https

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off 
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,R=301,L]
RewriteRule ^((?!subdirectory/).*)$ /subdirectory/$1 [L]

为主域更改 example.com ,为子域更改 subdirectory目录

This will remove www prefix , and force https:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off 
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,R=301,L]
RewriteRule ^((?!subdirectory/).*)$ /subdirectory/$1 [L]

Change example.com for you main domain and subdirectory for Sub Directory

虐人心 2024-10-20 11:11:47

尝试

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]

Try

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文