HTACCESS - Change Directory Structure - wordpress

I recently changed our URL structure for several different pages but I'm having some trouble with the HTACCESS rewrites. I've included the examples below and I'm hoping that someone can help me with the correct rule to use!
http://www.tintworld.com/ny/albany-022/home-window-tinting/ <-- Original URL
http://www.tintworld.com/albany-ny-022/home-window-tinting/ <-- New URL ('ny' is moved)
I'm currently trying to use the following rule but it's not redirecting the original URLs...
RewriteCond %{HTTP_HOST} ^www\.tintworld\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^ny/albany-022/$ http://www.tintworld.com/albany-ny-022? [R=301,NE,NC,L]
Please help. :-)

The problem is likely due matching ^ny/albany-022/$ in your RewriteRule. The $ in regular expressions denotes the end of the line. Try removing the $, and adding the remaining part onto the rewritten URL
RewriteRule ^ny/albany-022/(.*) http://www.tintworld.com/albany-ny-022/$1
The $1 here puts in the part that's matched by the (.*).

if you have several different pages, I would recommend using a plugin like
http://www.weberz.com/plugins/404-redirected/
to keep track of 404 pages and redirect them

Related

.htaccess 301 with querystring and special characters

I've looked around the site for answers and followed a few examples but i'm still drawing a blank with this one.
I'm trying to get the following to redirect.
www.mydomain.com/?s=flat+roof+specifier+checklist
to
www.mydomain.com/resources/flat-roof-checklist/
My rewrite rule is below.
RewriteCond %{QUERY_STRING} s=flat+roof+specifier+checklist
RewriteRule ^(.*)$ /resources/flat-roof-checklist/? [L,R=301]
I think the issue im having is the "+" character.
if i change my URL to:
www.mydomain.com/?s=flat%20roof%20specifier%20checklist
and my rewrite condition to:
RewriteCond %{QUERY_STRING} s=flat%20roof%20specifier%20checklist
It works fine.
The problem with that is, the URL HAS to be www.mydomain.com/?s=flat+roof+specifier+checklist (with the + characters in there) as the business has already sent out communications with that particular url, so unfortunately i can't change it.
Am i missing some sort of regx to format my url beforehand.?
There's already a 301 redirect plugin running on the site but it doesn't work with that either.
Apologies as i'm fairly new to php,wordpress,htaccess etc.
Any help will be great appreciated.
You can place the pattern into a regex capture group and escape the plus signs, tested and working on my test server with the following
RewriteCond %{QUERY_STRING} (s=flat\+roof\+specifier\+checklist)
RewriteRule ^(.*)$ /resources/flat-roof-checklist/? [L,R=301]

redirection 301 with htaccess with variables in url

I've searched answers on the forum but didn't worked for me.
I have rebuild an website using Wordpress and want to keep old urls.
old url:
/bingo/play-bingo-40/review?reviews_id=16
nwe url:
https://website.com/product/the-new-url-of-play-bingo-40/
I have tried this:
RewriteCond %{QUERY_STRING} ^reviews_id=([^&]+) [NC]
RewriteRule ^bingo/play-bingo-40/review https://website.com/product/the-new-url-of-play-bingo-40/? [R=301]
Any one?
You don't use anything from the query string, so you don't need to capture with a RewriteCond. You seem to use part of the request URL, so you must capture this one
RewriteRule ^bingo/(.+?)/review https://example.com/product/the-new-url-of-$1/? [R,L]
To learn more about mod_rewrite generally and regular expressions particularly, see http://httpd.apache.org/docs/current/rewrite/intro.html. Another good reference for regular expressions is http://www.regular-expressions.info/

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.
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
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.
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

Doing 301 redirects of urls containing a specific string caused by hacking

I have a wordpress site that was hacked and hundreds of urls were created that no longer exist after I cleaned the site up. I have noticed a lot of these urls contains a specific keyword as displayed below
http://www.capecrossfit.com/kamagra-holland/ - I need to rewrite all urls containing the word kamagra to http://www.capecrossfit.com/ - I have tried all the recommendations on near similar requests on this site with no joy, can anyone please advise how this can be done with htaccess?
Pace this rule as very first rule (just below RewriteEngine On line):
RewriteEngine On
RewriteCond %{THE_REQUEST} /kamagra-holland|buy-kamagra-europe|kamagra-super-jelly [NC]
RewriteRule ^ / [L,R=301]

Bulk .htaccess Redirect - query string to WordPress

I'm going to need to redirect about 700 urls that look like this...
mydomain.com/index.php?id=3&d_id=8297
..to the new appropriate page in a wordpress site.
Assuming that I change the new wordpress slug to something that includes the 4 digit number,
is there a way to do this in .htaccess using a formula (so I don't have to do it for each one separately)?
Thanks
You can use this rule as your very first rule in WP .htaccess file:
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=[0-9]+&d_id=([0-9]+) [NC]
RewriteRule ^ /%1/? [R=301,L,NE]
I found a solution to my problem... the URLS that I need to change were in a Zeemap. Zeemaps allows you to export and import data through a spread sheet. I can make the new URLS mydomainname.com/1234 and use find and replace on the first portion of the URL.
Me again. I ended up using the .htaccess to redirect the pages to the WordPress search results for the page number.
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=[0-9]+&d_id=([0-9]+) [NC]
RewriteRule ^ /?s=%1 [R=301,L,NE]
Thanks for your help!

Resources