I have three versions of an article on a website. Each one is written a few years after the last and updates the topic to keep it fresh and I want to redirect traffic from the older ones to the newer ones. However, I Still want people to be able to visit the older ones via links in each updated article. How can I use .htaccess to redirect external traffic to the newest article but allow links to the older ones to still work?
2 Answers
There is one simple way of doing this:
First: Create the redirects.
Redirect permanent /articles/old-article-1.html http://www.example.com/articles/new-article.html
Redirect permanent /articles/old-article-2.html http://www.example.com/articles/new-article.html
Second: Rename old articles so that you can link to them.
> mv old-article-1.html archive-article-1.html
> mv old-article-2.html archive-article-2.html
This way, any reference to the old articles will go to the new article while you will still be able to link to the old article. This is the simplest solution.
-
That's what I thought. I had hoped to avoid relocating the old articles as the CMS URL scheme follows the pattern domain.tld/item/{$ID} I had hoped to use some sort of ?btw=noredirect but I guess I will have to make it work. Thanks. Sep 20, 2014 at 17:45
It took a bit of reading and a lot of trail and error to get right but with the help of https://stackoverflow.com/questions/7218164/multiple-rewriterules-for-single-rewritecond-in-htaccess and also a few other sites (listed at the end) this is my solution which seems to work in enough cases to satisfy me.
RewriteCond %{HTTP_REFERER} www.lordmatt.co.uk [OR]
RewriteCond %{HTTP_REFERER} lordmatt.co.uk [NC]
RewriteRule .? - [S=4]
RewriteRule ^item/1739$ http://lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/1739/$ http://lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/1739\?(.*)$ http://lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/1739/\?(.*)$ http://lordmatt.co.uk/item/2429/ [R=302,L]
RewriteCond %{HTTP_REFERER} www.lordmatt.co.uk [OR]
RewriteCond %{HTTP_REFERER} lordmatt.co.uk [NC]
RewriteRule .? - [S=4]
RewriteRule ^item/966$ http://lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/966/$ http://lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/966\?(.*)$ http://lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/966/\?(.*)$ http://lordmatt.co.uk/item/2429/ [R=302,L]
What this is doing is testing to see if the referer is my site and if it is it skips the next four rules which will redirect even badly formed URLs (that the CMS will still correctly handle) (first rule being missing slash and the last two being URL cruft).
This means however that those edge cases that refuse to send the referer header will just get sent back to the newest article. I used a 302 redirect so that the redirect is not cached and visitors from redirected links can still go back and see the old stuff if they want.
It also accounts for those odd cases where people insist on putting a www on my site name (something I do not like) and although another rule stips that off if it is still standing for any reason the skip will still work.
Also of massive help were
-
No problems. I was thinking that a well thought out pattern could probably reduce those rules down to one rule per archive page. Sep 21, 2014 at 20:22