Score:1

How Can We Do 301 Redirect Of Over 200 Pages To Corresponding 200 Pages At New Location?

vn flag

I need help and need it fast.

We recently migrated a subdomain to integrate with the domain.

Eg. https://blog.example.com to https://www.example.com/blog

https://blog.example.com has a ton of traffic which we don't want to lose. So we need to do 301 redirects.

The problem is that https://blog.example.com has over 200 pages which are performing well.

I need advice how we can redirect all the pages to their corresponding pages at the new location without slowing down the website or overloading the server.

We tried

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

But only the home pahe was redirected

Do we have to redirect each of those pages one by one?

Score:0
kz flag

We tried

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

You said you "recently migrated a subdomain", however, the code you posted does not target a subdomain and conflicts with the example hostnames you described in the question?

This will redirect "only the home pahe" if you have put it in the wrong place in the .htaccess file. eg. If you have placed it after an internal rewrite to a front-controller. This redirect would need to go at the very top of your root .htaccess file.

without slowing down the website or overloading the server.

200 like-for-like URL redirects are insignificant in terms of performance/overhead. However, to completely minimise all impact these redirects might have then the blog subdomain should point to a different area of the filesystem, which simplifies the redirect further. (In fact, you don't need to point the subdomain at any particular directory and instead perform the redirect directly in the server config / virtual host, not .htaccess.)

Your "200 redirects" can then be performed with a single mod_alias Redirect directive (in .htaccess or <VirtualHost> container). For example:

Redirect 301 / https://www.example.com/blog/

The Redirect directive is prefix-matching and everything after the match is copied to the end of the target URL. So, for example, http(s)://blog.example.com/anything is 301 redirected to https://www.example.com/blog/anything.

Otherwise, if your subdomain and main domain point to the same place on the filesystem then use mod_rewrite to check the requested hostname, much like as you did initially (although not sure why you were checking for example.com or www.example.com when you should have been checking for the subdomain blog.example.com).

For example, at the very top of the root .htaccess file:

 RewriteEngine On

 RewriteCond %{HTTP_HOST} ^blog\.example\.com [NC]
 RewriteRule (.*) https://www.example.com/$1 [R=301,L]

The NC flag is superfluous since the regex .* already matches everything.

Or, use the REQUEST_URI server variable instead in the RewriteRule directive and avoid the backreference. For example:

 :
 RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

The REQUEST_URI server variable already includes the slash prefix.

Test first with a 302 (temporary) redirect to avoid potential caching issues. And make sure you have cleared your browser cache before testing.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.