Apache Conditional RedirectMatch - apache

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.

Related

Alternative for bulk Redirect 301 in htaccess

I have some link redirects in htaccess in which every link should be added with /en after .com i.e.,
mydomain.com, mydomain.com/abc.html
to
mydomain.com/en, mydomain.com/en/abc.html
After some research i found that redirect 301 does it. But I think this is not perfect and permanent solution. In future if I add another URL, again I have to add it to htaccess. So can any one help me out in getting the perfect and permanent solution for this.
I have done this in my htaccess
Redirect 301 /about.html /en/about.html
Redirect 301 /contact-us.html /en/contact-us.html
Redirect 301 /index.html /en/index.html
Redirect 301 /locations.html /en/locations.html
You can use this single redirect rule in your site root .htaccess:
RewriteEngine On
RewriteRule ^(?!en/)(.+\.html)$ /en/$1 [L,NC,NE,R=301]
(?!en/) is negative lookahead to assert that URI is not already starting with /en/.

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

htaccess redirect subdirectory to root

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/ /

.htaccess redirect for parent but not children?

I have a number of URLs that I need to redirect to new locations, but there are some situations where child pages need to remain active and not redirected. For example:
/products would redirect to http://www.newsite.com/products
/products/category1 would redirect to http://www.newsite/products/category1
But /products/specialitem would not get redirected at all.
Is this possible with either Redirect or RedirectMatch?
Doing a Redirect 301 /products http://www.newsite.com/products seems to affect all child pages
Thanks for any guidance!
Edit:
Using waynethec's answer, I was able to get started. But can anyone clarify why my first rule below works but the others do not?
RedirectMatch 301 ^segment-one$ http://www.google.com/
RedirectMatch 301 ^segment-one/segment-two$ http://news.google.com/
RedirectMatch 301 ^segment-one/segment-two/segment-three$ http://cnn.com/
RedirectMatch 301 ^segment-one/segment-two/segment-three/foobar$ http://gbv.com/
(By not working, I mean that I still can get to the pages, rather than them getting redirected.)
You should be able to use the following RedirectMatch rule:
RedirectMatch 301 ^/products$ http://www.newsite.com/products
Note that this will only redirect requests for /products, not /products/, or /products/pagename.extension.
You can use RedirectMatch:
RedirectMatch 301 ^/products(?!/specialitem)(.*)$ http://www.newsite.com/products$1
This will redirect /products, or anything after it, except /products/specialitem/
If you need to add conditions, this also worked for me. Note, I had to eliminate the slash between ^ and products when using RewriteRule.
RewriteCond [...whatever your conditions are...]
# test with 302 first to avoid annoying caching issues
# RewriteRule ^products$ http://www.newsite.com/products [R=302,NC,L]
# but use 301 in production once you know it's working
RewriteRule ^products$ http://www.newsite.com/products [R=301,NC,L]