htaccess redirect url with space in it - .htaccess

Stupidly, I have sent out a newsletter without checking the links. One of which is broken so I want to handle this with htaccess.
My URL that is being linked to is:
http://www.domain.com.au/www.domain.com.au/campers-and-motorhomes/ne%20w-zealand/camper-rentals/
where the actual page is:
http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/
Note the space in new zealand as well as the additional www.domain.com.au
How can I set this up in htaccess?
Thanks

Since you don't have to manipulate the URL, you can use a simple Redirect:
Redirect /www.domain.com.au/campers-and-motorhomes/ne%20w-zealand/camper-rentals/ http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/
Edit If Apache doesn't like the space unquoted as %20, try quoting the whole thing with a real space in there:
Redirect "/www.domain.com.au/campers-and-motorhomes/ne w-zealand/camper-rentals/" http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/
Edit2 If it's appending a query string, you will need to use mod_rewrite to get rid of the querystring rather than a simple redirect, I'm afraid.
RewriteEngine On
# If the request starts with www.domain.com.au, it is the broken link
# Rewrite to the proper URL and put ? on the end to remove the query string
RewriteRule ^www\.domain\.com\.au http://www.domain.com.au/campers-and-motorhomes/new-zealand/camper-rentals/? [L,R=301]

Related

.htaccess RewriteRule with an extra query string

I have changed the path of some of my urls, and I'd like to redirect them to some new urls with an extra query string ?industry=
Old url: e.g. https://domain.ext/skills/keywords/list.php?q=account+manager
https://domain.ext/skills/keywords/list.php?q=assistant
New url: e.g.
https://domain.ext/skills/keywords/list.php?industry=human-resources&q=account+manager
https://domain.ext/skills/keywords/list.php?industry=banking&q=account+manager
https://domain.ext/skills/keywords/list.php?industry=banking&q=assistant
As you can see the new URls contains a new query parameter industry= which can have different industries.
So how can I check if a new url exist with the query ?q=account+manager for example and then redirect the old url to one of the new url that match &q=account+manager?
I've looked into RewriteRule with .htaccess, but I haven't been able to find the right redirection yet. If someone could share some thoughts. Thanks.
Not sure what you wish to do, but I will take this question as a request for a simple example of a Rewrite Rule. Depending on what you are trying to achieve, several approaches may be used.
The code in the .htaccess file could be like this:
# essential
Options +FollowSymLinks
RewriteEngine On
# Here a simple rule, the link ending with assitant will be sent to list.php as the q parameter
RewriteRule ^skills/keywords/assistant$ list.php?q=assistant
# You may use variables and Regex, such as $1, $2, $3 in this case to receive the values
# in the parentheses for the three parameters
RewriteRule ^(en/fr/es)/(keywords)/([\/\.\w_-]{3,})$ list.php?lang=$1&section=$2&q=$3 [L,R=301,NC]
Flags are these ones:
NC = No Case
R=301 = returns a 301 (permanent redirection) code rather than a
302 (temporary) for SEO purposes
L = Last ruel to be executed,
next ones are ignored.
I haven't check if this code works, when testing this on your server, you will encounter a blank page if something is wrong. Also, if you use flags, make sure there is no whitespace after the comma.
This page explains all this very well: https://httpd.apache.org/docs/trunk/en/rewrite/intro.html

how to redirect URL containing query string and random numbers in .htaccess

When customers cancel a transaction on my site, they get redirected to the WooCommerce cart page with a query string containing randomly generated numbers at the end.
Example
https://www.example.com/cart/?woo-paypal-cancel=true&token=EC-5474842406066680S
(I need this redirect due to a plugin conflict between WP Rocket cache with CDN activated and WooCommerce. Long story.)
I'm wondering what exactly I would put in my .htaccess file to get it to redirect to
https://www.example.com/cart/
I've tried a number of variations I found on multiple pages here on Stackpath, but it wasn't redirecting. Obviously I'm missing something so I'm turning to the gurus.
Would be very grateful for your help.
To redirect /cart/?woo-paypal-cancel=true&token=<anything> to /cart/ you can try something like the following near the top of your .htaccess file (using mod_rewrite):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^woo-paypal-cancel=true&token=
RewriteRule ^cart/$ /cart/? [R,L]
The ? on the end of the RewriteRule substitution strips the query string from the request.
This is a temporary (302) redirect.

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

301 redirect URL with query string, removing part of the beginning URL, keeping the full query at the end

I'm trying to find a redirect that will remove part of a URL (in the middle), but leaves the query string in place at the end.
I can do this fine via a single url redirect, but there are hundreds of these urls so I'm trying to find a rule that might be able to do for all of them in one fell swoop, so i don't have to make one for each and any new ones will get redirected automatically.
I'm trying to remove 'search.php' from the urls, here is an example:
www.site.com/products/search.php?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
to redirect to:
www.site.com/products/?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
Thanks for your time.
According to the documentation, the query string is passed through unchanged by default
RewriteRule products/search.php http://www.site.com/products/ [L,R=301]

Remove trailing ? [question mark] from url - empty query string

I am moving a website to a new Open Cart install and I have set up 301 redirects to keep any SEO value.
I had to use the following trick here - http://www.itsadam.co.uk/opencart-301-redirect-not-working-seo-fix-route/ - to work around an annoying issue with the way Open Cart handles url rewrites. So, I have this sort of thing in my .htaccess file currently:
redirect 301 /products-page /products?
However, this now leaves my rewritten urls with a trailing ? - an empty query string effectively:
http://www.mysite.com/products?
Is there a way I can use rewrites to match and ditch any superfluous question marks? (I need to retain any actual query strings).
I encountered the same issue in opencart. Rather than follow the link to your solution do it like this:
RewriteRule ^original-url$ new-url [L,R=301]

Resources