URL Redirection if only certain URL is matched htaccess - .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

Related

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 with wildcard and not recursive

I've consolidated about 20 old pages into one new page, and want to redirect web links going to those pages to the new page.
I started out listing each one in htaccess as a Redirect 301, but thought I might save processing time to do a wildcard string match instead. Unfortunately it failed, because I suspect the page I want to go to is also caught in the wildcard.
For example I want to redirect, www.mydomain.com/catalog/listname_oranges.php, listname_lemons.php, listname_figs.php etc to redirect to www.mydomain.com/catalog/listname_addons.php
So I tried this, which failed:
RedirectMatch 301 ^/catalog/listname_.*$ /catalog/listname_addons.php
How do I fix this so its not recursive?
You can use a negative lookahead in your regex:
RedirectMatch 301 ^/catalog/listname_(?!addons\.php).*$ /catalog/listname_addons.php
This way, the listname_addons.php file won't match the regex but everything else will.

htaccess rewrite regex for pagination urls

I need an expression that would rewrite (actually 301 redirect) old url patterns ended with -0.htm, to new ones -1.htm
so if old url was /path/to/pagination/script-0.htm
new url must be /path/to/pagination/script-1.htm
This must work for any number of folder/subfolder structure.
And must work only for [-0.htm]. Only for urls that end with this pattern.
Any idea on how to accomplsh this?
My htaccess regex knowledge is limited to none.
You could try something like:
RewriteRule ^(.*?)\-0\.htm$ $1-1.htm [R=301,L]
But make sure it's one of the first rules, before the ones that might do a pass-through if a real file.

301 Redirect Adding Query to End of Target URL

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

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