htaccess redirect subdirectory to root - apache

What rule can I add .htaccess that would give me the following result?
I want to redirect to the homepage any search that includes a certain directory. For example, when anyone tries...
test.com/example
...or...
test.com/example/something
...they end up at...
test.com
Most everything I have tried kinda worked but always ends up appending whatever was after the targeted directory to the domain after the redirect, for example...
test.com/example/something
...leads to...
test.com/something
This is not what I want. Thanks
Code I have tried...
RewriteRule ^example/(.*)$ http://www.test.com [L,R=301]
and
Redirect 301 /example http://www.test.com
UPDATE
This seems to fix it...
RedirectMatch 301 ^/example/ /
Redirect 301 /example http://www.test.com/
Thanks!

Try:
RewriteEngine On
RewriteRule ^example/ / [L,R=301]
or
RedirectMatch 301 ^/example/ /

Related

htaccess 301 redirects to wrong url

So I did a 301 redirect with htaccess but dont understand why the final link is wrong?
This is what I end up with http://example.com/about?/about-us/
Rather than a clean intended http://example.com/about
This is my htaccess code
Redirect 301 /about-us/ /about
To me this looks fine... What have I done wrong?
I need to redirect from http://www.example.com/about-us/
I've also tried
Redirect 301 /about-us/ http://example.com/about
You can try using apache mod_rewrite instead of using Redirect:
RewriteEngine on
RewriteBase /
RewriteRule ^about-us/?$ /about [L,R=301]

htaccess Redirect excluding pagination

this is related to: Apache Conditional RedirectMatch
I am redirecting /blog to the root (/)
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/(.+)$ /$1
This works correctly:
/blog/ - no redirection
/blog/foo/ - redirects to domain.tld/foo/
/blog/foo/bar/ - redirects to domain.tld/foo/bar/
Etc.
I would like to make one modification. My pagination urls are domain.tld/blog/page/1, domain.tld/blog/page/2, etc and they should not redirect.
How can I prevent /blog/page/ from redirecting?
Thank you!
Try :
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/((?!page/[0-9]).+)$ /$1
This will not redirect
/blog/page/id

Apache Conditional RedirectMatch

I have moved a website from /blog/ to the web root. I created a RedirectMatch 301 for my URLs which I believe is correct:
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/(.*)$ /$1
This works correctly. Now, domain.tld/blog/foo/ redirects to domain.tld/foo/
However, there is one issue. There is a page on the website with the slug: /blog/
Now you can't access the page since domain.tld/blog/ redirects to domain.tld/
So what I would like to accomplish is not to redirect /blog/, but to redirect /blog/everything/else/
domain.tld/blog/ no redirection
domain.tld/blog/foo/ redirects to domain.tld/foo/
domain.tld/blog/foo/bar/ redirects to domain.tld/foo/bar/
Etc.
Many thanks for your help!
Can you try this? I think it needs a little change:
RewriteEngine On
RewriteBase /
RedirectMatch 301 ^/blog/(.+)$ /$1
Remember to clear cache + history on browsers before testing, browsers remember 301 redirect rules.
Hope that helps.

.htaccess rewrite using RedirectMatch

I need to redirect several URL to one
FROM
1.example.com/groups/3d/
2.example.com/groups/3d/skype:softlist_ua?chat
3.example.com/groups/3d/+/
4.example.com/groups/3d/+/+/
5.example.com/groups/3d/+/skype:softlist_ua?chat
TO
example.com/catalog/3d/
Redirect statement works fine, however when old URL containts get reuqests, it adds this get request to end of the new URL. How can I remove it?
RedirectMatch 301 ^/groups/3d/.*$ /catalog/3d/$1
Try:
RedirectMatch 301 ^/groups/3d/.*$ /catalog/3d/?
or
RewriteEngine On
RewriteRUle ^groups/3d/ /catalog/3d/? [L,R=301]

Website article .htaccess 301 redirect

I have a site in a folder called patients and my urls look like so:
http://site.com/patients/post-name
http://site.com/patients/articles/another-post-name
I want to redirect them all to a clean:
http://newsite.com/post-name
http://newsite.com/another-post-name
In other words, lose the /articles/ which sometimes appears and 301 to the new site.
Any help on how to do this with htaccess?
Edit your .htaccess in http://site.com server and put this(must be the first rule):
RewriteEngine On
RewriteRule .* http://newsite.com/ [R=301]
or
Redirect 301 / http://newsite.com/
Regards.