htaccess redirect with wildcard and not recursive - .htaccess

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.

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

How do I format this htaccess rule to not redirect a specific query?

I am using an htaccess rule to redirect all urls in the format of:
www.example.com/blog/the-name-of-the-post
to
www.example.com/the-name-of-the-post
So I've removed "blog" from the URL. The redirect rule below is working. However I do not want to redirect any URLs in the format of:
www.example.com/blog/page/x
So if "/page" appears after "blog", then I don't want to do the redirect. The problem is I'm also redirecting the blog pages when paginated.
RedirectMatch 301 ^/blog/([^/]+)/([^/]+)/$ https://www.example.com/$2
In sum,
www.mysite.com/blog/the-name-of-post redirects to www.example.com/the-name-of-post
www.example.com/page/1 (or page/2, page/3, etc) do not redirect
Thanks
If you don't want to redirect when page appears after blog, you can do a negative match for the word page, like so:
RedirectMatch 302 /blog/(?!page).*$ https://www.example.com/
I'm not entirely clear on what all the different possibilities are from your description so I'm just giving you an example of not matching when the word page appears in the second position of the URI.

htaccess redirect with folder name

I need some help with htaccess redirects. Out site was wrongly crawled by google and as a result there are a lot of wrong urls being shown in webmaster tool. As an example:
articles/abcd/xyz
should be redirected as
articles/abcd/
articles/abcd/xyz.php
should be redirected as
articles/abcd/
articles/abcd/xyz.html
should be redirected as
articles/abcd/
So basically I am trying to mean always redirect to articles/abcd/ for varous wrong url types that i shown above. Please could you help
You can simply use RedirectMatch from mod_alias (documentation). We assume that the part after articles does not contain any / character. We redirect with a temporary redirect to the url without the suffix. Change "temp" to "permanent" after testing that this redirect actually works as you expect it to work.
RedirectMatch temp ^(/articles/[^/]+/).+$ $1

htaccess redirecting from rewritten dynamic urls to new dynamic urls

i am experiencing a very unique problem and i hope someone can help!
so we have recently created a new ecommerce website and we made it live and everything was working great but when we to implement our 301's from our old pages we were getting some wierd things
so the code below actually works
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
this code does not
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
the output when i try to do this redirection is "Invalid parameters specified!" on a blank webpage and in the address bar it has this
http://mysite.com/index.php?cat=1/sub_directory/
we were thinking that maybe the problem is because our old pages were dynamic but mod_rewrite was used to create more readable urls and we have also deleted all our old files because they were interfering with our new pages rendering
any help would be greatly appreciated!
thanks
That is strange, as redirect should only match the specific url listed, where as it looks like its behaving like rewriterule and partially matching the subdirectory url against the first rule..
try putting the more specific rule above the less specific, like so:
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
That way the more specific rule will be hit first, and the /directory/ only rule will only match if more specific matches above fail
alternatively, you could try RewriteRules:
RewriteRule ^directory/$ http://mysite.com/index.php?cat=1 [R=301,NC,L]
RewriteRule ^directory/sub_directory/$ http://mysite.com/index.php?cat=2 [R=301,NC,L]
the ^ and $ anchors should prevent any unwanted partial matching

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