Mass 301 redirect in htaccess
-
I use ScreamingFrog to generate sitemaps for my Magento 2 multistore, but I recently noticed two issues.
- Each category/page has two URLs. One with / and the end and one without.
- Every product has two URLs. One with /product-name and the other /shop/product-name.
The URLs are canonicalised, but this is still a problem and I'm not sure exactly how to execute this in the htaccess file.
So I need to:
- Remove all URLs without the / at the end and redirect them all to the URL with / at the end. Or vice versa.
- 301 redirect every single product (there are over 400) from shop/product-name to /product-name.
How do I do this en mass in the htaccess file?
-
Hi Martijn,
The solution didn't work, I'm not sure if there is a conflict here but this is what my htaccess currently looks like:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]#RewriteCond %{HTTPS} !=on
#RewriteRule ^(.)$ https://thespacecollective.com/$1 [L,R=301]
#RewriteRule ^(.)$ https://www.thespacecollective.com/$1 [L,R=301]<ifmodule pagespeed_module="">ModPagespeed off</ifmodule>
RewriteRule .* - [E=noabort:1]
RewriteRule .* - [E=noconntimeout:1]
<ifmodule mod_rewrite.c="">RewriteEngine On
RewriteCond %{HTTP_HOST} ^interstellarstore.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.interstellarstore.com$</ifmodule>RewriteRule (.*)$ https://www.thespacecollective.com/$1 [R=301,L]
-
This is solid advice
If OP has access to NginX redirects (usually requires shell access, and obviously NginX) which are generally deemed to be faster and more efficient than .htaccess rewrite rules, they could do something like
rewrite ^([^.]*[^/])$ $1/ permanent;
I got it from here: https://stackoverflow.com/questions/645853/add-slash-to-the-end-of-every-url-need-rewrite-rule-for-nginx
"The Regular Expression translates to: rewrite all URIs without any '.' in them that don't end with a '/' to the URI + '/'"
Not all sites run NginX, but many do
-
1. So for the first one what you want to do is a redirect and add the trailing slash. The .htaccess lines that you want to put in for that are:
RewriteCond %{REQUEST_URI} !(/$|.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
HSTS Redirects
Hi Are these 307 redirects bad for SEO? They've just popped up on an audit & I haven't seen them before. I'm guessing as they're temporary they should be updated. Thanks Becky
Intermediate & Advanced SEO | | BeckyKey0 -
How to handle potentially thousands (50k+) of 301 redirects following a major site replacement
We are looking for the very best way of handling potentially thousands (50k+) of 301 redirects following
Intermediate & Advanced SEO | | GeezerG
a major site replacement and I mean total replacement. Things you should know
Existing domain has 17 years history with Google but rankings have suffered over the past year and yes we know why. (and the bitch is we paid a good sized SEO company for that ineffective and destructive work)
The URL structure of the new site is completely different and SEO friendly URL's rule. This means that there will be many thousands of historical URL's (mainly dynamic ones) that will attract 404 errors as they will not exist anymore. Most are product profile pages and the God Google has indexed them all. There are also many links to them out there.
The new site is fully SEO optimised and is passing all tests so far - however there is a way to go yet. So here are my thoughts on the possible ways of meeting our need,
1: Create 301 redirects for each an every page in the .htaccess file that would be one huge .htaccess file 50,000 lines plus - I am worried about effect on site speed.
2: Create 301 redirects for each and every unused folder, and wildcard the file names, this would be a single redirect for each file in each folder to a single redirect page
so the 404 issue is overcome but the user doesn't open the precise page they are after.
3: Write some code to create a hard copy 301 index.php file for each and every folder that is to be replaced.
4: Write code to create a hard copy 301 .php file for each and every page that is to be replaced.
5: We could just let the pages all die and list them with Google to advise of their death.
6: We could have the redirect managed by a database rather than .htaccess or single redirect files. Probably the most challenging thing will be to load the data in the first place, but I assume this could be done programatically - especially if the new URL can be inferred from the old. Many be I am missing another, simpler approach - please discuss0 -
Google ranking 301 redirected vanity urls
We use vanity URLs for offline marketing. An example vanity URL would be www.clientsite.com/promotion, this URL 301 redirects to a page on the site with tracking parameter ex: www.clientsite.com/mainpage?utm_source=source&utm_medium=print&utm_campaign=xyz. We are running into issues with Google ignoring the 301 redirect and ranking these vanity URLs instead of the actual page on the website. Any suggestions on how to resolve?
Intermediate & Advanced SEO | | digitalhound0 -
Preserve domain on 301 redirect?
We have a domain solely used for print advertising that does a 301 redirect to a landing page (a department home page) on our "real" domain that is indexed on Google. Example: www.bmwrepairs.com redirects to www.repairshop.com/bmwrepairs. Is there a way to do a 301 redirect so that when they get redirected, the URL in the browser address bar remains www.bmwrepairs.com?
Intermediate & Advanced SEO | | Jazee1 -
301 redirects aren't passing value.
We recently migrated our shop to a new platform. We are using Wordpress for our main website, but we wanted a separate installation of Wordpress for our shop, so we left the main blog where it was, but moved the shop to a /shop/ sub directory with it's on WP installation. So now we have 2 installations of Wordpress. However, since we've done this, none of the pages on the new shop are ranking for anything. Their page rank is 0, and Moz page authority is 1 for every page on the new site. I've set up the proper 301 redirects, and they're redirecting fine, but none of the page value is coming over. It's been about a week now, and despite re-crawls by google, I'm not seeing any change. Also, one of the original (now re-directed) product pages still has a Page Authority of 13 according to Open Site Explorer. I know it's not high, but it had us ranking in the top 5 for a very important keyword, and now that value is being wasted. For example, one of our product pages that was ranking well was startupfashion.com/product/fashion-brand-line-sheet-template
Intermediate & Advanced SEO | | inkyj
That page is now redirected to
http://startupfashion.com/shop/product/fashion-line-sheet-template I've done 301's plenty of times and I've never seen this issue, so i'm wondering if it could have something to do with having multiple installations of Wordpress. I can't see any obvious issues with it... i have the Yoast SEO plugin configured properly on both installations, and all of the pages ARE being indexed by google. Not sure what is going on. Anyone have any experience with this, or have any ideas? Thanks!!0 -
Should You Use 301 Redirects When Switching To A Secure SSL Server?
Hi, our client has switched from a non-secure server to a secure (SSL) server.. but the non secure pages still exist, i.e. http://www.stainlesshandrailsystems.co.uk/balustrade-systems.html (non-secure)
Intermediate & Advanced SEO | | Webpresence
https://www.stainlesshandrailsystems.co.uk/balustrade-systems.html (secure) We assumed that we should 301 redirect the http pages to the new https pages using the following htaccess rule; RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R,L] HOWEVER! both of the above pages show the same Page Authority (PA) and Pagerank (PR).. does this mean that they are being seen as the same page, do we really need to employ 301 redirects? Many thanks in advance, much appreciated. 🙂 Lee1 -
How to do a site migration followed by a domain migration and avoid 301 redirect chains?
Hi all, The current roadmap for our Eng team has us performing a site migration (redirecting one subfolder to another subfolder) and then a domain migration shortly after. The way I see it, I have 2 scenarios (the 1st involves the site migration THEN the domain migration and the 2nd is the site migration and domain migration being done simultaneously): olddomain.com/subfolder-old to olddomain.com/subfolder-new THEN olddomain.com/subfolder-new to newdomain.com/subfolder-new AND olddomain.com/subfolder-old to newdomain.com/subfolder-new olddomain.com/subfolder-old to newdomain.com/subfolder-new I also understand that there are two best practices for a domain migration and they are 1) keep everything the same that you can to help Google understand it is the same page, just on a different domain and 2) avoid chain redirects. As you can imagine, scenario 1 requires more Eng costs than scenario 2. So, my question is, is scenario 2 a perfectly viable option or should I make the push to go for scenario 1? Any advice is greatly appreciated!
Intermediate & Advanced SEO | | brad-causes1 -
301 a PDF?
Hi Website A is moving to Website B. Website A has a number of PDFs - obviously I cant do a '301 redirect' on them. Any suggestions what to do with the PDFs? Many thanks in advance Nigel
Intermediate & Advanced SEO | | Richard5550