Why htaccess redirect being overruled? - .htaccess

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.

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

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 redirect partial url pattern

I am having issues with writing the proper redirect statement, and unfortunately can't rap my head around the syntax needed. Other questions have similar problems, but I can't figure out how to properly reuse the information in other posts.
I have a url: www.site.com/.../CORE_Testing_5010 this unique.
I need the page to redirect to www.site.com/core-phase
The ... could be multiple directories /a/b/c/d/CORE_Testing_5010 or just /a/CORE_Testing_5010
Right now, I have 310 redirects for most of the possible directory combinations, but that is inefficient.
Some guiedence and explanation would be helpful.
Try:
RedirectMatch 301 /CORE_Testing_5010$ /core-phase
The regex pattern ignored everything before the last /, so there can be anything in front of /CORE_Testing_5010.
If you have rewrite rules, and using mod_alias (the RedirectMatch) is causing conflicts, then you can stick with mod_rewrite:
RewriteRule /?CORE_Testing_5010$ /core-phase [L,R=301]
and it needs to be before any rules that do routing.

301 correct way to redirect a directory

I was wondering what would be the correct way to do a redirect like this:
Redirect 301 /about/$ http://domain.com/new-about/
Redirect 301 /about$ http://domain.com/new-about/
Redirect 301 /about/me http://domain.com/new-about/
That's how I currently do it and it works but I believe there should be a better way?
I have 2 pages which are rewrited, one page is a subpage of the other page /about/me/, now both pages should redirect to the new page new-about
You should either use RedirectMatch or use mod_rewrite to perform regular expression matching. This should work and would be the shortest and best performing solution:
RedirectMatch 301 ^/about /new-about/
Domain and protocol are optional - leaving them out like this will keep them as they were, so this is future proof.
Been a while since I asked this question and I am now more comfortable with regular expressions.
The way I would do this now is:
Redirect 301 ^/about http://domain.com/new-about/
A tool which helped me a lot understanding regular expressions and which I would advice anyone trying to understand them is Rubular: http://rubular.com/

Resources