I want to be able to have the an URL be reformed in an htaccess file and redirect a file that is no longer present.

Taking this:


and having this be renamed to:


I've used the following code

RewriteEngine On

Redirect 301 /foo/thing.htm /foo/

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI}

The problem I have had is that when the Redirect 301 happens, the RewriteCond is skipped according to https://htaccess.madewithlove.com/

Redirect 301 /foo/thing.htm /foo/

The new url is http://www.example.com/foo/

Test are stopped, a redirect will be made with status code 301

and the output is http://www.example.com/foo/

How can I get all of my commands in my htaccess file be ran so that the URL is processed properly in addition to any other additional commands such as cache control and webp compatability?

  • Shouldn't http://www.example.com/foo/ then redirect to HTTPS? It isn't usually a problem to have a short chain of redirects. Commented Sep 18, 2023 at 6:11
  • Yes. That's what I need help with. The output is incorrect in my htaccess Commented Sep 18, 2023 at 21:39
  • Have you tried using rewrite rules for everything rather than a redirect? Commented Sep 18, 2023 at 21:53
  • No I haven't. That's not a bad idea. I have just been using scripts that I've found online, tbh. Commented Sep 18, 2023 at 22:43

1 Answer 1


The problem is in the order of things.

You have basically 3 commands:

  1. Redirect using a 301 code from /foo/thing.htm to /foo/
  2. If the url is http, redirect to https
  3. (If the url is http, OR, has www.) AND (optionally has www.) then redirect to https without www

Note that once a rule is triggered, the rest aren't. Therefore going to:


is only triggering rule #1, and redirecting to http://www.example.com/foo/

Also, rule #3 is unnecessary complex, since rule #2 already does it.

I would rewrite your .htaccess as this:

RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}  [L,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI}  [L,R=301]

Redirect 301 /foo/thing.htm /foo/
  • Thanks! I will try this. Commented Sep 30, 2023 at 0:02

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.