htaccess is redirecting my wordpress wp-content folder too by mistake

ok, so i’m relatively new to .htaccess and redirects but I’ve been searching high and low for this solution and it’s driving me crazy.

Right now I’m redirecting my old blogger blog to wordpress.

Read More

So, I have this code in place:

RedirectMatch 301 /2014/01(.*) /journal/trip1/$1

This redirects:

www.website.com/2014/01/BlogTitle

to:

www.website.com/journal/trip1/BlogTitle

This all works great EXCEPT it’s also redirecting my images in the “wp-content/uploads/2014/01” folder because it matches the “2014/01” in the redirect

The image should be here:

http://www.website.com/wp-content/uploads/2014/01/SomeImage.jpg

Instead it redirects to here:

http://www.website.com/journal/trip1/SomeImage.jpg

Like I said, I’ve searched high and low for solutions and I’m sure it’s something ridiculously easy.

Any help greatly appreciated!

Thanks
Todd

Related posts

2 comments

  1. As mentioned in the comment by @Hobo, you are not checking from the beginning of the URI. This means that anything before /2014/01 will also be matched. As such, you’ll need to add ^ before the /2014. It should now look like this:

    RedirectMatch 302 ^/2014/01/(.*)$ /journal/trip1/$1
    

    As a rule of thumb, it’s also better to end the expression with $.

    Alternatively, you can use mod_rewrite to do the redirect. Using the condition and rule below, you can do the redirect whilst ensuring the requested URI does not map to an existing file.

    RewriteEngine on
    
    # Make sure the request does not map to an existing file:
    RewriteCond %{REQUEST_FILENAME} !-f
    
    # If so, redirect:
    RewriteRule ^2014/01/(.*)$ /journal/trip1/$1 [R=302,L]
    

    Also, per @Hobo’s comment, you should always do a temporary redirect instead of a permanent one when testing. Once you’re happy with either of the above (i.e. they’re working as expected) change both 302s to 301s.

  2. What if you apply the solution said by @MikeRockett and @Hobo, but, instead denying any found file

    RewriteCond %{REQUEST_FILENAME} !-f
    

    deny only the images on your URI

    RewriteCond %{REQUEST_URI}  !(.jpg|.jpeg|.png|.gif)$
    

    Not sure if it’s the real solution. I’d say that in a comment, but I’ve got not enough points yet

Comments are closed.