Making a 301 or 302 redirect really is not something that someone use or think will use a lot or very frequently. However, over time perhaps one or another event may arise that leads to the need to do so, for one or another circumstance.
As we said, for this we will only need to add at the end of our Apache .htaccess a line of code that will do all the work.
Redirect only one page
If we are only going to redirect a page to a new domain or subdomain, we can enter this code:
redirect 301 /url_antigua.html http://www.dominio-nuevo.com/url-nueva/
To do this, it is sufficient to indicate the url of origin in a relative way, while the url of destination must be indicated in an absolute way.
Redirect all pages to a single page
When creating a web one can create many pages or post. If we want each and every one of them to direct to a single url of destination, we can do it in a massive way. Example of this would be:
miweb.com/1 to example.com
miweb.com/2 a ejemplo.com
miweb.com/3 a ejemplo.com
miweb.com/4 at example.com
As we can see, there is a massive redirection, from all the URLs of one domain to the root of another domain. And we do it by just attaching to .htaccess this:
redirectMatch 301 ^ (. *) $ http://www.desarrolloweb.com
Paired, or paired, redirection
Now, maybe we have the same post and the same URL in the origin and destination, maybe in another domain or subdomain, and we want to do the redirection in bulk.
An example of how this would work is like this:
miweb.com/1 to example.com/1
miweb.com/2 to example.com/2
miweb.com/3 at example.com/3
miweb.com/4 at example.com/4
Well, the solution is very simple. Simply attach this code snippet:
redirectMatch 301 ^ (. *) $ http: //example.com$1
You can redirect to either a domain or a subdomain or subdirectory, including a subdirectory.
In this post we have put the examples with code 301 (which indicates that the url has been moved permanently); However, if it is only temporary, we can change 301 to 302. That’s it. I hope it has served you and, importantly, it has been easy to understand.
June 18, 2019