I'm trying to redirect address:


to address:


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.


2 Answers 2


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?


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.

  • You're absolutly righht. I have 2 htaccess files. One in root directory , one in blog (subdirectory). In roots' htacess I've rules that do not apply to redirects I need (vide from blog with no slash and no https, to blog with slash and https on). In blogs' htacess I've rules that apply to redirects I need (blog is a wordpress one).
    – Sargit
    Commented Apr 4, 2017 at 6:29
  • As I've mentioned I need redirect from addres example.com/blog to address example.com/blog. Even better would be redirect to address example.com/blog It doesn't need to be oneliner in htaccess. If I use DirectorySlash Off and RewriteOptions AllowNoSlash would that be a security concern? I'll accept your answer, cause it's the closest to what I need.
    – Sargit
    Commented Apr 4, 2017 at 6:29
  • Btw. www in an address is desired prefix. So it's all ok, not a typo.
    – Sargit
    Commented Apr 4, 2017 at 6:34
  • I've updated my answer with an example and concerns about DirectorySlash Off.
    – MrWhite
    Commented Apr 4, 2017 at 12:35

Not sure if you meant 'one line' or 'one redirect' (or rewrite in your case). Since you already have only one RewriteRule, I assumed that you are aiming for a one-liner:

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

That rewrites every HTTP-request that starts with /blog and either ends there or has a 'trailing slash' to https://example.com/blog/

  • That rule doesn't work if blog is directory and htaccess is withing blog subdirectory, that this rule applies to.
    – Sargit
    Commented Apr 4, 2017 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.