htaccess Redirect excluding pagination - apache

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

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]

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.

Redirecting old site / subfolders to new site / subfolders

I am attempting to write some redirects and have stumbled on my final step of redirecting folders.
I have an old site with the following structure,
example.com
example.com/example
example.com/example/example
example.com/example/example
(with the domain examples.com parked and redirecting to this site)
And a new site (the domain unparked),
examples.com
examples.com/examples/new
examples.com/examples/examples/updated
examples.com/examples/examples/sample
Using a traditional 301 on the original site as per the below simply redirects to root
redirect 301 /example/ http:www.examples.com/examples/new/
it will not point to the correct subfolder
As a short term I have used the below to redirect all links to the root (on the old server) but it would be ideal to push them all to their exact locations
RedirectMatch 301 ^/example/.*$ http://www.examples.com/
I'm obviously missing something simple so any suggestions are appreciated.
Try this:
RedirectMatch 301 /example(.*) http://examples.com/examples/new/$1
RedirectMatch 301 /example/example(.*) http://examples.com/examples/examples/$1
Example #1: http://oldsite.com/example/XXXX should redirect to http://newsite.com/examples/new/XXXX
Example #2: http://oldsite.com/example/example/UPDATE should redirect to http://newsite.com/examples/examples/UPDATE
ok if those above don't work..try this..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite\.com [NC]
RewriteRule ^example/?$ http://newsite.com/examples/new [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite\.com [NC]
RewriteRule ^example/example/([^/]+)/?$ http://newsite.com/examples/examples/$1 [R=301,L]
</IfModule>

redirect certain pages to its equivalent in another domain, otherwise redirect to its homepage

I want to use htaccess to redirect certain pages to its equivalent in another domain, other pages redirect to homepage.
Like that:
http://old-domain.com/certain-page redirected to: http://new-domain.com/certain-page
http://old-domain.com/another-certain-page redirected to: http://new-domain.com/another-certain-page
Only these pages will be redirected, otherwise, pages have to be redirected to the new domain home page.
http://old-domain.com/non-certain-page redirected to: http://new-domain.com
This is my try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
</IfModule>
But don't know how to exclude other pages.
Any help here?
Thanks in advance!
I'm not sure what you mean by
But don't know how to exclude other pages
But you are already excluding the pages when you create specific rules for them. This should work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#redirect specific pages
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
#Redirect everything else to homepage
RewriteRule ^(.*)$ http://new-domain.com/ [R=301,L]
</IfModule>
Let me know how this works for you. If you need to clear you cache before trying these new rules.
Another answer was mentioned here, thank to anubhava
With redirect without need to use Rewrite :
RedirectMatch 301 ^/.+ http://www.newdomain.com/
So, the answer can be also:
#Redirect specific pages
redirect 301 /certain-page http://new-domain.com/certain-page
redirect 301 /another-certain-page http://new-domain.com/another-certain-page
#Redirect everything else to homepage
RedirectMatch 301 ^/.+ http://new-domain.com

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