301 Redirect Problems

I have some redirects to do and I’m having problems:

As an example I want to redirect this page:

https://www.theenlightenedhound.com/gifts-dog-lovers/dog-is-my-co-pilot-print

to this page:

https://www.theenlightenedhound.com/gifts-dog-lovers/

I have tried the following code in my htaccess file:

RedirectMatch 301 /gifts-dog-lovers/dog-is-my-co-pilot-print(.*) /gifts-dog-lovers/$1

and this works BUT if I try to redirect this page (with a / at the end)

https://www.theenlightenedhound.com/gifts-dog-lovers/dog-is-my-co-pilot-print/

the redirected page changes to:

https://www.theenlightenedhound.com/gifts-dog-lovers//

which has an extra / at the end and doesn’t exist

I’ve also tried this code in the htaccess file:

Redirect 301 /gifts-dog-lovers/dog-is-my-co-pilot-print/ /gifts-dog-lovers/

and this does the reverse - ie it works for this page (with a / at the end)

https://www.theenlightenedhound.com/gifts-dog-lovers/dog-is-my-co-pilot-print/

but generates a 404 error for this page:

https://www.theenlightenedhound.com/gifts-dog-lovers/dog-is-my-co-pilot-print

I’ve also tried lots of redirect and rewrite code to try and get rid of the // but nothing is working.

Any help please - its doing my head in!! I have no HTML knowledge . Thank you all

Okay,
Are you trying to redirect a lot of pages?

Are there similarities in the page names or structure?

For the single page (or any sub-pages) you could try this:

RedirectMatch 301 ^/gifts-dog-lovers/dog-is-my-co-pilot-print /gifts-dog-lovers$1

That would take any page name that starts with /gifts-dog-lovers/dog-is-my-co-pilot-print and redirect it to /gifts-dog-lovers/ and add the single / at the end. Also, it would take any subpages and do the same.

So if you have a page named /gifts-dog-lovers/dog-is-my-co-pilot-print/sample/ it would redirect it to /gifts-dog-lovers/sample/`.

1 Like

@teefers Hi Doug - thanks so much for your reply … I’ll test this tomorrow and come back to you - much appreciated :):+1:

I’ve got about 10 pages to redirect
Some of them have similarities…

The example I gave you is redirecting a single page (with no subfolder)s… to a folder (gifts-dog-lovers) that has sub pages

I also have examples where I’m redirecting a specific page to another specific page… like this one…

https://www.theenlightenedhound.com/gifts-dog-lovers/dog-sayings-word-art

which will be redirected to:

https://www.theenlightenedhound.com/gifts-dog-lovers/inspirational-dog-wisdom-life-quotes-prints

Would code for this one be as follows???

RedirectMatch 301 ^/gifts-dog-lovers/dog-sayings-word-art /gifts-dog-lovers/inspirational-dog-wisdom-life-quotes-prints

Though I do have several subpages on this one to redirect to the same place… these are the subpages…

https://www.theenlightenedhound.com/gifts-dog-lovers/dog-sayings-word-art/print-1
https://www.theenlightenedhound.com/gifts-dog-lovers/dog-sayings-word-art/print-2
https://www.theenlightenedhound.com/gifts-dog-lovers/dog-sayings-word-art/print-3

all to redirect to

https://www.theenlightenedhound.com/gifts-dog-lovers/inspirational-dog-wisdom-life-quotes-prints

so would that same one piece of code do the trick for all those??

Thanks
Debbie

That looks like it should work.

This should redirect all the pages starting with https://www.theenlightenedhound.com/gifts-dog-lovers/dog-sayings-word-art/ to the single page https://www.theenlightenedhound.com/gifts-dog-lovers/inspirational-dog-wisdom-life-quotes-prints/

RedirectMatch 301 ^/gifts-dog-lovers/dog-sayings-word-art/(.*) /gifts-dog-lovers/inspirational-dog-wisdom-life-quotes-prints/
2 Likes

Hi @teefers I tested the code this morning. The ‘single page to folder with subpages’ and the ‘single page to single page’ redirects worked fine however I couldn’t get the ‘multiple sub pages to single page’ code to work for all possible pages - this was the code…

RedirectMatch 301 ^/gifts-dog-lovers/dog-sayings-word-art/(.*) /gifts-dog-lovers/inspirational-dog-wisdom-life-quotes-prints/

I tested it on the excellent htaccess code tester as recommended by @joeworkman

It worked for these URLs
https://www.theenlightenedhound.com/gifts-dog-lovers/dog-sayings-word-art/
https://www.theenlightenedhound.com/gifts-dog-lovers/dog-sayings-word-art/print-1

but not for this one

https://www.theenlightenedhound.com/gifts-dog-lovers/dog-sayings-word-art

I guess because there was no / at the end?? but that is a possible URL that could be searched so I have to address it

So I just wrote a 301 for each of my single pages separately to be sure all bases are covered … not too much of an issue as there are only 15

The great thing with the code you shared is not just that it works :ok_hand: but also that it doesn’t generate the extra / on the redirected page if the user puts a / at the end of the page url they were looking for… so that is great -thanks so much

So in summary, if this helps anyone else, then this is the code from Doug that I used:

Redirecting a single page to a folder with subpages:

RedirectMatch 301 ^/folder-name/page-name /new-folder-name$1

in my case this was the following…

RedirectMatch 301 ^/gifts-dog-lovers/dog-is-my-co-pilot-print /gifts-dog-lovers$1

Redirecting a single page to another single page

RedirectMatch 301 ^/folder-name/page-name /new-folder-name/new-page-name

in my case this was the following…

RedirectMatch 301 ^/gifts-dog-lovers/dog-sayings-word-art /gifts-dog-lovers/inspirational-dog-wisdom-life-quotes-prints

Thank you so much again for your help. I had a massive headache yesterday after a full day of trying to sort this out so I am very happy today!!

leaving breadcrumb

Similar question.

I’ve moved my blog from catholichusband.org to catholichusband.com.

My blog posts are indexed on Google in two formats:

^/index_files/blog-post.php

and

^/?post=blog-title

I set up my RedirectMatch rules to the following:

RedirectMatch 301 ^/index_files/(.*) https://catholichusband.com/index_files/$1

RedirectMatch 301 ^/?post=(.*) https://catholichusband.com/index_files/$1

My .htaccess also includes the following Rewrite rule:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^catholichusband.org [NC,OR] 
RewriteCond %{HTTP_HOST} ^www.catholichusband.org [NC] 
RewriteRule ^(.*)$ https://catholichusband.com/$1 [L,R=301,NC]

Can you see where I’ve gone wrong?

I’m a little confused. You said you are redirecting the blog. Aren’t you redirecting the entire site?

The old site .org would need the redirect to the new site. Looks like you have a rewrite for that?

I can’t get to the link you gave above http://catholichusband.org/. It redirects me to the .com version.

I would need to see EVERYTHING you have in both htaccess files right now to help.

Hey Doug! Thanks for jumping in.

You’re right, I’m redirecting the entire site. The site is mostly the blog.

I used to do this using 2 rules in Cloudflare, but I moved to a new host and can’t proxy the DNS A records without messing up the Let’s Encrypt auto renewal process.

Here’s the .org .htaccess file:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^catholichusband.org [NC,OR] 
RewriteCond %{HTTP_HOST} ^www.catholichusband.org [NC] 
RewriteRule ^(.*)$ https://catholichusband.com/$1 [L,R=301,NC]


RedirectMatch 301 ^/index_files/(.*)$ https://catholichusband.com/index_files/$1

RedirectMatch 301 ^/?post=(.*) https://catholichusband.com/index_files/$1

Here’s the .com .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]


ErrorDocument 404 /404.php

Redirect 301 /grant-us-peace/ https://grantuspeacebook.com

Redirect 301 /the-transition/ https://transitionmarriagebook.com


<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  mod_gzip_item_exclude mime ^image/.*
  mod_gzip_item_include handler ^cgi-script$
</ifModule>

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING

I’m still at a loss of what isn’t working and what is?

Not sure what you are talking about here? Google indexes the entire URL. If you are getting to the same page with different URL’s then that’s not good.

Perhaps some real examples of what you have and what you want. Post a URL from the old site and a URL to the new site where you want to end up.

On the old site since it is going away you don’t need anything to do with www or https,. It would be simplest to just redirect everthing to the new site.

So I’d change the old sites htaccess to this:

RewriteEngine on 
RewriteRule ^(.*)$ https://catholichusband.com/$1 [R=301,L]

That will remove www and force https, so everything that goes to the old site will be sent to the new with no www and set to use the https.

I started blogging on this website in 2013 and, over the years, have moved between all of the various blogging options. That, unfortunately, left me with a checkered history of blog URL structures indexed on Google, many of which still show up in results. I’m trying to make sure they don’t 404 in the event that someone comes across them. Then last year I was able to pick up the .com, which added to the complexity!

I’ve updated the old .org htaccess to the single Rewrite rule that you provided.

Here are two examples for you, one from each of the different URL schemes (/index_files/blog-post.php & /?post=blog-post) :

Google result:

I’d like to redirect it to:

Google result:

I’d like to redirect to:

Are you sure? It doesn’t seem to be working.

The First URL
https://www.catholichusband.org/index_files/the-impact-of-a-great-confession.php

Should get redirected to
https://catholichusband.com/index_files/the-impact-of-a-great-confession.php

With the Rule above.

The second URL
https://www.catholichusband.org/?post=your-sacred-ring
Should get redirected to:
https://catholichusband.com/?post=your-sacred-ring
The ends up here without a 404.
https://catholichusband.com/index_files/your-sacred-ring.php

Now if you want to remove the query string ?post=your-sacred-ring and make it a file name that can be done. I don’t know if it’s worth the work, depending on how many blog entries you have.

Doug,

I always appreciate your willingness to help out here on the forums!

Since I’m just using the .org as a forwarding/redirect url, I’ve decided to just rely on CloudFlare to do the forwarding with my previously used rules and not worry about full SSL on the domain. It looks like I’m up and running again.

Hope you are well!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.