Forum Moderators: phranque
Here's the problem:
I switched my website from a static HTML site to a custom WordPress theme and had to 301 about a hundred pages.
For some reason, the redirect is not working at all for a few pages that are in the same folder. It's forgetting to add a slash in the middle of the url and it's adding a ".html" to the end of the page.
Old: www.site.com/reviews/product-one-review.html
New: www.site.com/all-product-reviews/product-one
The problem is the redirect is sending users here:
www.site.com/all-product-reviewsproduct-one-review.html
Here's the code:
Redirect 301 /reviews/product-one-review.html [mysite.com...]
After close inspection, it looks like my 301 is sending visitors to some mixed up jumble of my old address and new address.
What gives?
Oh and here's the top portion of my .htaccess. This probably has something to do with it:
(Note - the first chunk of code here is something that came with my custom theme. It redirects to affiliate sites so that I can track clicks and make the affiliate urls look a lot prettier)
RewriteEngine On
RewriteRule ^go/([^/]*)\.htm$ /?hrtrooms=$1 [L]
RewriteRule ^(.*)index\.html$ $1 [R=301,L]
RewriteEngine On
RewriteRule ^redirect/([^/]*)$ /?hrtbanners=$1 [L]
RewriteEngine On
RewriteRule ^download/([^/]*)\.htm$ /?droom=$1 [L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# protect the htaccess file
<files .htaccess>
order allow,deny
deny from all
</files>
# disable the server signature
ServerSignature Off
# protect wpconfig.php
<files wp-config.php>
order allow,deny
deny from all
</files>
# disable directory browsing
Options All -Indexes
# set the canonical url
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ [mysite.com...] [R=301,L]
RewriteEngine On just once at the very beginning. Delete the repetition that appears after the initial one. Something we give out as advice several times per week is ""If you use RewriteRule for *any* of your rules use RewriteRule for *all* of your rules"". Do not mix RewriteRule with Redirect or RedirectMatch in the same .htaccess file). So, change your rules to use RewriteRule instead.
Make sure that you list all of the redirects before all of the rewrites - otherwise you will end up re-exposing rewritten internal filepaths back out into the URL.
Make sure that your general non-www to www redirect is the last redirect listed, and is therefore shown immediately before the first of the rewrites.
You have one redirect that currently does not have the domain name specified in the target URL (it is also listed in the middle of a bunch of rewrites). Add the domain name in the rule target and move the rule so that it is listed somewhere before your general non-www to www redirect.
Some of your code hasn't been commented.
Thanks for the awesome reply and breaking everything down.
Repetition of the RewriteEngine has been removed.
I'm going to try to do some more research but if you feel like dumbing it down for me a little, I have 3 Qs:
1. Mixing redirect and rewrites. Is this a problem with all my 301s? I don't see any redirects in the code I pasted but it sounds like you do.
2. Listing redirects before rewrites. So does this mean I should move all 100 301's to the top of my htaccess file?
3. Which redirect are you talking about that doesn't have the domain name specified? I'm not seeing any redirects except for my 301s.
Thanks!
I can tell by how hard these questions were to frame that they are pretty newbish. I've been Googling my ass off the last couple of days but info has been pretty sparse. I really shouldn't be trying to write my own .htaccess files but it was unavoidable in this case.
I do appreciate it very much though.
Take care.
Redirect 301 /reviews/product-one-review.html http://www.example.com/all-product-reviews/product-one in there. Move the redirects (the lines with [R=301,L] in) to be before the rewrites.
This redirect
RewriteRule ^(.*)index\.html$ $1 [R=301,L] is missing the domain name. In that case, there would be a two step redirect if a request for non-www index name was received - one redirect to fix each issue. That is a bad move as it creates a "redirection chain". You need to fix both issues in the same redirect. That's why a redirect always includes the domain name. All the redirects must be listed first from the most specific redirect affecting only one page, or a small number of pages, to the most general last (non-www to www). The redirects are then followed by your rewrites, again from most specific to most general.
There are tens of thousands of previous threads with example code in them. It is well with looking at others that have a thread title that looks like it might cover some of the same topics. it will give you a head start with code examples that can be modified, as well as many tips as to why things are coded as they are.