Combine multiple 301 redirects
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, wordpress, 301-redirect, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :I need to combine multiple redirects via my .htaccess in Wordpress for future site migration and re-launch:
- Domain change
- Force HTTPS
- Force www
- URL structure changes (important to keep juice here)
I'm wondering if there's any order this is preferably done? Page to page redirects before or after domain and HTTPS+www? Does it matter? And is the below code correct?
RewriteEngine On
# Force HTTPS, WWW URLs, and change of domain
RewriteCond %HTTP_HOST ^(www.)?olddomain.com$ [NC]
RewriteRule ^ https://www.newdomain.com%REQUEST_URI [L,NE,R=301]
RewriteCond %HTTPS off
RewriteRule ^ https://%HTTP_HOST%REQUEST_URI [L,NE,R=301]
## 301 Redirects
# Page to Page
Redirect 301 pages/page-name https://www.newdomain.com/page-name
Redirect 301 collections/products/product-1 https://www.newdomain.com/product/product-1
# BEGIN WordPress
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%HTTP:Authorization]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule . /index.php [L]
# END WordPress
You've definitely got the right idea, and yes, page-to-page first.
Do the page-to-page redirects first and use the absolute correct URL. That way, anyone hitting a page that has to be redirected will bypass the other steps because they don't meet the conditions.
Your reference to keeping the juice means that these page-to-page redirects are the most important to you, so make sure they're done first and done right. None of these will then have more than one redirect.
Then do the old => new domain redirect and the force HTTPS that you have already, in the order you already have them. This will pick up any traffic not hitting a page-to-page redirect and will still only redirect once.
Doing it the other way around means anyone coming to an old page on the new site (which is possible) would hit multiple redirects, and that's something to try to avoid.
Comments
Post a Comment