.htaccess 301 with querystring and special characters - wordpress

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]

Related

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/

Apache HTAccess Remove Query String for redirect

It sounds like a relatively simple one but here goes.. I have a wordpress website called example.com It has recently been redeveloped using wordpress however the old sites structure used the following www.example.com/?page_id=22 I want to redirect anything after the domain name that contains /?page_id=[any digit] to the homepage, im having great difficulty in accomplishing this and I also think I may have issues because wordpress itself uses the query string page_id before the permalinks redirects kick in, I could be wrong though but any help on removing the query string directly after a domain name would be great especially keeping in mind that it is a wordpress install.
I have tried various iterations that look like the following to no avail
RewriteCond %{QUERY_STRING} ^page_id=(.*)$
RewriteRule (.*) http://www.example.com/ [R=301,L]
RedirectMatch 301 ^/page_id=(.*)$ /
Try this rule right at top, just below RewriteEngine On line:
RewriteCond %{QUERY_STRING} ^page_id=\d+$ [NC]
RewriteRule ^/?$ /? [R=301,L]
/? will strip off any query string.

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]

HTACCESS - Change Directory Structure

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

Using htaccess to rewrite special characters for Wordpress url aliases

I recently migrated a fairly large site (~6,000 posts) from Drupal to Wordpress. As part of the process, I migrated the Drupal-created url aliases to Wordpress for SEO and link retention purposes.
An example of a url alias that Drupal created that worked great in Drupal:
/stories/will-this-be-another-la-niña-year
That url in Wordpress returns a 404. However, this works:
/stories/will-this-be-another-la-nina-year
It seems then my best bet is to write a generic international character to english character set rewrite rule in htaccess, before the url is passed to Wordpress.
Any idea how I might do this?
Thanks a lot for whatever help you can give.
Matt.
It seems like there might be a better way to do this within wordpress, you may want to do a quick browser through the wordpress Trac tickets, there maybe some patch or temporary fix for the problem. But if you need to go to an htaccess/redirect method, you can either use a RewriteMap to sanitize and redirect-if-needed or explicitly redirect on non-ascii characters.
A RewriteMap requires access to either server or vhost config to setup the map. It could be as simple as a list of /stories/will-this-be-another-la-niña-year URIs mapped to http://yourdomain.com/stories/will-this-be-another-la-nina-year (the all ascii URL, the http:// is significant because it tells mod_rewrite to redirect the browser). Or you can write a script to look for non-ascii characters and replace them with the appropriate ascii character.
Text mapping:
RewriteMap sanitize txt:/path/to/uri_mapping.txt
Script mapping:
RewriteMap sanitize prg:/path/to/sanitize_script.php
Then in your htaccess file, you can invoke this mapping like this (these rules will need to be above the wordpress rules, since you want the URI sanitized before wordpress gets a hold of them.
RewriteRule ^(.*)$ /${sanitize:$1|$1} [L]
If you don't have access to server/vhost config, you'll have to enumerate the possibilities in your htaccess file, again putting these rules above the wordpress rules:
# replace ñ
RewriteRule ^(.*)ñ(.*)$ /$1n$2 [R=301,L]
# replace ú
RewriteRule ^(.*)ú(.*)$ /$1ú$2 [R=301,L]
etc.
I just added the following lines at the beginning of my .htaccess file and it works:
RewriteRule ^(.*)é(.*)$ /$1e$2 [R=301,L]
RewriteRule ^(.*)è(.*)$ /$1e$2 [R=301,L]
RewriteRule ^(.*)ê(.*)$ /$1e$2 [R=301,L]
RewriteRule ^(.*)î(.*)$ /$1i$2 [R=301,L]
RewriteRule ^(.*)ô(.*)$ /$1o$2 [R=301,L]
RewriteRule ^(.*)û(.*)$ /$1u$2 [R=301,L]
RewriteRule ^(.*)â(.*)$ /$1a$2 [R=301,L]
RewriteRule ^(.*)à(.*)$ /$1a$2 [R=301,L]
RewriteRule ^(.*)ï(.*)$ /$1i$2 [R=301,L]

Resources