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]
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
-
301 Redirects for Multiple Language Sites in htaccess File
Hi everyone, I have a site on a subdomain that has multiple languages set up at the domain level: https://mysite.site.com, https://mysite.site.fr , https://mysite.site.es , https://mysite.site.de , etc. We are migrating to a new subdomain and I am trying to create 301 redirects within the htaccess file, but I am a bit lost on how to do this as it seems you have to go from a relative url to an absolute - which would be fine if I was only doing this for the english site, but I'm not. It doesn't seem like I can go from absolute url to an absolute url - but I could be wrong. I am new to editing the htaccess file - so I could definitely use some advice here. Thanks.
Intermediate & Advanced SEO | | amberprata0 -
301 redirect hops from non-https and www
It's best practice to minimize the amount of 301 redirect hops. Ideally only one redirect hop. It's also best practice to 301 redirect (or at least canonical) your non-https and/or your non-www (or www) to the canonical protocol/subdomain. The simplest (and possibly the most common) way to implement canonical protocol/subdomain redirects is through a load balancer or before your app processes the request. Both of which will just blanket 301 to the canonical domain/protocol regardless if the path exists or not In which case, you could have: Two hops. i.e. hop #1 http://example.com/foo to https://example.com/foo, hop #2 https://example.com/foo to https://example.com/bar 301 to a 404. Let's say https://example.com/dog never existed, but somebody for whatever reason linked to it (maybe a typo). If I request https://www.example.com/dog, the load balancer would 301 to a 404 page. Either scenario above should be fairly rare. However, you can't control how people link to you. Should I care about either above scenario? I could have my app attempt to check if the page exists before forwarding, but that code could be complicated.
Intermediate & Advanced SEO | | dsbud0 -
How to speed up transition towards new 301 redirected landing pages?
Hi SEO's, I have a question about moving local landing pages from many separate pages towards integrating them into a search results page. Currently we have many separate local pages (e.g. www.3dhubs.com/new-york). For both scalability and conversion reasons, we'll integrate our local pages into our search page (e.g. www.3dhubs.com/3d-print/Bangalore--India). **Implementation details: **To mitigate the risk of a sudden organic traffic drop, we're currently running a test on just 18 local pages (Bangalore) = 1 / 18). We applied a 301 redirect from the old URL's to the new URL's 3 weeks ago. Note: We didn't yet update the sitemap for this test (technical reasons) and will only do this once we 301 redirect all local pages. For the 18 test pages I manually told the crawlers to index them in webmaster tools. That should do I suppose. **Results so far: **The old url's of the 18 test cities are still generating > 99% of the traffic while the new pages are already indexed (see: https://www.google.nl/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:www.3dhubs.com/3d-print/&start=0). Overall organic traffic on test cities hasn't changed. Questions: 1. Will updating the sitemap for this test have a big impact? Google has already picked up the new URL's so that's not the issue. Furthermore, the 301 redirect on the old pages should tell Google to show the new page instead, right? 2. Is it normal that search impressions will slowly shift from the old page towards the new page? How long should I expect it to take before the new pages are consistently shown over the old pages in the SERPS?
Intermediate & Advanced SEO | | robdraaijer0 -
What to do about old urls that don't logically 301 redirect to current site?
Mozzers, I have changed my site url structure several times. As a result, I now have a lot of old URLs that don't really logically redirect to anything in the current site. I started out 404-ing them, but it seemed like Google was penalizing my crawl rate AND it wasn't removing them from the index after being crawled several times. There are way too many (>100k) to use the URL removal tool even at a directory level. So instead I took some advice and changed them to 200, but with a "noindex" meta tag and set them to not render any content. I get less errors but I now have a lot of pages that do this. Should I (a) just 404 them and wait for Google to remove (b) keep the 200, noindex or (c) are there other things I can do? 410 maybe? Thanks!
Intermediate & Advanced SEO | | jcgoodrich0 -
404 ? 301 ? What is your opinion ?
Hi, I have a classifieds website and I am wondering about the life of a page with an ad. An announcement has therefore a limited life, so : Is a 404 pages? a 301 redirect to the section? let the content without redirection? What is your opinion? Sorry for my english, i'm french 😉 Thanks. A.
Intermediate & Advanced SEO | | android_lyon0 -
301 redirects.
Hi everyone, I am having some issues with an a few dynamic URLs that are not redirecting; Example: http://www.example.com/shop-online?page=shop.product_details&flypage=flypage_images.tpl&product_id=69164&category_id=303 I first tried to carry out a standard 301 which looked like this; Redirect 301 /longurlwith&category_id=303 http://www.example.com/new-url Which didn't work. After a little bit of research I added the following into the htaccess file; RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]RewriteRule ^/shop-online$(.*)$ http://www.example.com/shop-online$ [NE,L,R=301] Which caused the website to error 500 (Not cool). So now I am stumped. Any help would be really appreciated as I'm sure it's an easy fix but I can't quite my finger on it. Thanks in advance :).
Intermediate & Advanced SEO | | AduroLabs0 -
301 doesn't redirect a page that ends in %20, and others being appended with ?q=
I have a product page that ends /product-name**%20** that I'm trying to redirect in this way: Redirect 301 /products/product-name%20 http://www.site.com/products/product-name And it doesn't redirect at all. The others, those with %20, are being redirected to a url hybrid of old and new: http://www.site.com/products/product-name**?q=old-url** I'm using Drupal CMS, and it may be creating rules that counter my entries.
Intermediate & Advanced SEO | | Brocberry0 -
301 or 302 Redirects to Mobile Site
When it's detected that a mobile device is accessing the site it has the ability to redirect from www.example.com to m.example.com. Does it make more sense to employ a 301 or 302 redirect here? Google says a 301 but does not explain why (although usually I stick to "when in doubt, 301") . It seems like a 302 would prevent passing link juice to the mobile site and having mobile-optimized results also showing up in Google's index. What is the preference here?
Intermediate & Advanced SEO | | SEOTGT0