We moved a part of our site from one sub folder to another. I want to put permanent redirects (301) into htaccess for the files in this folder (some have changed their filename as well, so I can't just setup one rule for the whole folder). Here's what I'm trying

RewriteRule ^search/tutorial-search.html$ db/tutorial.php [R=301]

This doesn't work though, I get a 404 response when now entering the old URL. I find this curious as I had a rule in place for ages that does work, which looks like this:

RewriteRule ^search/tutorial-search.html$ search/tutorial-search.php

I really don't see the big difference. I also tried the following (among others) but it doesn't work either

RewriteRule ^search/tutorial-search.html$ db/tutorial.php

What exactly is causing this to fail? Just to make sure I put all of these at the exact same line of the htaccess file. Is it because I'm rewriting to another folder? Thanks :)

  • Where's the htaccess file located that has these directives? Is it all in the same htaccess, or have you split them up to different directories? – Shane Madden Sep 25 '12 at 1:11
  • Actually posting your htaccess file so we can review it might be useful to figuring out that you're doing wrong, FYI. – HopelessN00b Oct 7 '12 at 18:28

1) Please post your entire .htaccess

I'm guessing you don't have any of these wrapped in the proper directives.

If you want to redirect the traffic, you shouldn't be using a Rewrite rule as this just rewrites the URL. What you really want is Redirect rule

RedirectMatch permanent oldfolder/oldfile.html "http://www.mysite.com/newfolder/newfile.html" ...... (Insert each one that has changed named) RedirectMatch permanent /oldfolder/* "http://www.mysite.com/newfolder"

That will give you the 301 redirects you are looking for. Rewrite actually changes the URL that the browser is requesting. Redirect repoints the URL and tells the browser/search engine, "Hey, this stuff moved. It's over here now."

  • I can't post the complete .htaccess. It should be possible to analyse the matter without the complete htaccess anyways as a) it's a very early section 2) as I said, the ReWrite I had in place (the 2nd one) works if placed in exactly the same line in the htaccess. So it's not a problem like not having the Rewrite Engine on or whatever. Also, what you say about redirects and rewrites isn't exactly right. You can't even defined a "Redirect 301 ..." for dynamic pages (which I also have, this static one was just the first one I wanted to get working). A ReWrite with [R=301] IS a redirect 301. – janb Sep 24 '12 at 22:26
  • "If you want to redirect the traffic, you shouldn't be using a Rewrite rule as this just rewrites the URL." - This isn't strictly true. The RewriteRule directive of mod_rewrite is used for both internal rewrites and external redirects, depending on the flags used. And if you are already using mod_rewrite for other rewrites/redirects then you should continue to use RewriteRule for further redirects in order to avoid conflicts. Redirect belongs to mod_alias, and as such, executes at a different time during the request - this can lead to unexpected results. – MrWhite Dec 12 '16 at 23:43

You do not define the redirect as a 301. The "permanent" flag after the RedirectMatch does that in the Apache headers.

If you have a line that is working on that server in one place, and a line that is broken in another, you obviously have something missing.

Try copying the entire .htaccess file from the old location and removing all the rewrite rules but one. Then just edit that one.

And unfortunately without seeing what else you have or do not have in your .htaccess, it really is impossible to diagnose what is causing your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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