301 Redirect Adding Query to End of Target URL - .htaccess

I can't seem to find a solution that works for my particular situation, despite many others having similar issues. When I try to create a 301 redirect for a URL that has already been rewritten, the redirect works, but appends a query string to the end of the target URL, which references the URL to be redirected. For example:
Redirect 301 /dir1/dir2/dir3/ http://www.example.com/dir1/dir2/dir5/
results in
http://www.example.com/dir1/dir2/dir5/?&a=/dir1/dir2/dir3/

I don't believe the Redirect rule above is appending the QS params, so it is likely another rule in your .htaccess.
You also need to verify when the additional QS params are being added, before the first redirect or in a subsequent redirect. You can to this using an HTTP debugging proxy e.g. Fiddler
Alternatively, you can use the place the equivalent rule below at the top of your .htaccess, before any other rules and see if the extraneous QS params are still there.
RewriteRule ^dir1/dir2/dir3/$ http://www.example.com/dir1/dir2/dir5/ [NC,R=301,L]
If they are still present, something else in your .htaccess is matching http://www.example.com/dir1/dir2/dir5/ and adding the QS value
Posting the relevant portions of your .htaccess, or the entire thing if you can would help

Related

URL Redirection if only certain URL is matched htaccess

I have a very small and odd issue. I want to write a rule which allows me to redirect URL.
https://www.example.com/category/florists/
to
https://www.example.com/category/florists.html
but to keep in mind there are other URLs which will be made from the above like
https://www.example.com/category/florists/fl/miami.html
I wrote a rule in .htaccess but it is causing trouble to later URLs
Redirect 302 "/category/florists/" /category/florists.html
this rule works fine but for this URL
https://www.example.com/category/florists/fl/miami.html
it makes it like this
https://www.example.com/category/florists.html/fl/miami.html
how can I solve it?
The mod_alias Redirect directive uses simple prefix-matching and everything after the match is copied into the end of the target URL (which explains your undesirable and malformed additional redirects).
To match just that one URL you need to use a RedirectMatch directive instead which matches against a regular expression (not prefix-matching).
For example:
RedirectMatch 302 ^/category/florists/$ /category/florists.html

.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

301 Redirect Issues in .htaccess file

I've been digging through the archives and threads and can't seem to find anything that matches my 301 Redirect issue -
I am trying to redirect old links to a new site and a problem with the following:
This one works - Redirect 301 /food-service/ http://www.xxxx.com/food-services.html
This one does not - Redirect 301 /food-service/distribution http://www.xxxx.com/distribution.html
The one that does not work tries to redirect to - http://www.xxxx.com/food-services.htmldistribution/
Would you mind lending me your thoughts on what I can do?
Thank you all!
The documentation for Redirect says:
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
The behaviour that you do observe is as we would expect from the documentation. It goes through the Redirect directives, and chooses the first one that matches.
To get the correct behaviour, you have to list the most specific redirect first, and the least specific last.
If you would have rules for /a/b/c, /a/b and /a, then you list them in that order.

htaccess redirect url with space in it

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]

Why htaccess redirect being overruled?

I've got:
Redirect 301 /blog/?p=1 http://www.new-site.com/blog/2000/10/myslug/
which works fine, unless followed by:
RedirectMatch 301 ^/blog(/)?(.*)$ http://www.new-site.com/blog/$2
I've tried all kinds of versions, including RewriteRule, but nothing has worked. How do I keep the first specific rule, and write an "everything else keeps its request uri and query string" rule?
Thanks
Alright, assuming these are the only two lines, what I see is this:
Redirect 301 /blog/?p=1 http://www.new-site.com/blog/2000/10/myslug/
RedirectMatch 301 ^/blog(/)?(.*)$ http://www.new-site.com/blog/$2
These are basically saying the same thing, that is, on a match, permanently redirect all blog queries to the new site.
With the second one you're saying match from the beginning the string /blog with a possible slash, which you'll capture, and possibly more information, which you'll also capture, then just put all that information into blog/extra-picked-up-info. This may be part of the problem, or you may be able to get around it by reordering the directives, and seeing if the lower directive receives precedence.
RedirectMatch 301 /blog(?:/\?)?(.*)?$ http://www.new-site.com/blog/$1
Redirect 301 /blog/?p=1 http://www.new-site.com/blog/2000/10/myslug/
Otherwise, you're going to need to reexamine your URIs, and find something more uniquely identifying.

Resources