大网数据广告活动
网站运营与故障问答

Redirecting directory without slash to directory with slash and https

· 52CE 我爱测

I'm trying to redirect address: http://example.com/blog to address: https://example.com/blog/ Take into account that, I'm trying to redirect from NO HTTPS to HTTPS. So far, I achieved the desired result in 2 redirects. I want to …

问题原文

原帖链接 · Sargit · CC BY-SA 3.0

I'm trying to redirect address:

http://example.com/blog

to address:

https://example.com/blog/

Take into account that, I'm trying to redirect from NO HTTPS to HTTPS.

So far, I achieved the desired result in 2 redirects. I want to achieve it in one redirect.

I need also to mention, that blog is a physical directory.

I'm using this rule:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/blog/?$
RewriteRule (.*) https://www.example.com/blog/ [R=301,L]

It redirects from http://example.com/blog/ (no-HTTPS) to https://example.com/blog/ (HTTPS).

And it is .htaccess.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

I'm assuming the .htaccess file you are referring to is in the document root? ie. example.com/.htaccess?

It redirects from http://example.com/blog/ (no-HTTPS) to https://example.com/blog/ (HTTPS)

Maybe this is just a "typo", but... it doesn't. Your RewriteRule directive explicitly includes the www subdomain.

I achieved the desired result in 2 redirects.

Well, the code you posted is just one redirect, not two. So, where is the other redirect coming from? Ordinarily, mod_dir will trigger an implicit redirect from http://example.com/blog to http://example.com/blog/ if blog is a physical directory (which you say it is). However, the directive(s) you posted should override this behaviour, unless...

... You have another .htaccess file in the /blog subdirectory that uses mod_rewrite? If you have a WordPress blog in the /blog subdirectory then this is quite probable. If this is the case then your mod_rewrite directives in the parent .htaccess are being completely ignored - they aren't doing anything! (Which, incidentally, would explain why you have www.example.com in your RewriteRule directive but are seeing a redirect to example.com - although maybe that is just a "typo"? Please clarify.)

As mentioned, mod_dir will implicitly trigger a 301 redirect from /blog to /blog/ and then maybe WordPress itself is triggering the redirect from HTTP to HTTPS? That's your two redirects. Admittedly that's a bit of a guess, but without more information, that's all we can do. What does the network traffic report? Specifically, what redirects are you seeing?

Solution

If the above hypothesis is correct that it's a bit of a tough one! You could get around the double redirect by moving everything from /blog/.htaccess into the parent .htaccess file (and making the necessary adjustments). However, this might break WordPress to some extent - as WP will no longer be able to maintain the .htaccess file.

Alternatively, if you are on Apache 2.4+ then you can mess with DirectorySlash Off and RewriteOptions AllowNoSlash and move your directives into the /blog/.htaccess file. Although I kinda wonder whether this is really "worth it"?

UPDATE: For example, as mentioned above, you can move your redirect into the /blog/.htaccess file (near the top) and turn DirectorySlash Off and set RewriteOptions AllowNoSlash - this requires Apache 2.4+:

# Prevent mod_dir implicitly appending a slash on directories (via redirect)
DirectorySlash Off

# Allow mod_rewrite to function when there is no trailing slash
RewriteOptions AllowNoSlash

# Redirect http://example.com/blog to HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} ^/blog$
RewriteRule ^ https://www.example.com/blog/ [R=301,L]

This should do as you require - in a single redirect. However, I've modified your redirect to include requests for https://www.example.com/blog (ie. HTTPS + /blog no trailing slash), otherwise that will no longer be resolved correctly - presumably this is a requirement?

Also, your existing redirect does not redirect http://www.example.com/blog/<something> - presumably that is intentional?

As with all 301 redirects, your browser cache will need to be cleared before testing.

You should also note that with DirectorySlash Off, mod_dir will not append the trailing slash to any subdirectories - should they be accessed. You will need to manage this as required. This can result in "unexpected behaviour", which I think is the biggest concern. Test test test.

AFAIK the only security concern is if you have directory indexes enabled. So, to be sure, these should be disabled: Options -Indexes in your .htaccess file. See the DirectorySlash directive in the Apache Docs for more information.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。