: Redirecting from a link but allowing the link to still be visited with htaccess I have three versions of an article on a website. Each one is written a few years after the last and updates
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?
More posts by @Gonzalez347
2 Comments
Sorted by latest first Latest Oldest Best
It took a bit of reading and a lot of trail and error to get right but with the help of 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} lordmatt.co.uk [OR]
RewriteCond %{HTTP_REFERER} lordmatt.co.uk [NC]
RewriteRule .? - [S=4]
RewriteRule ^item/1739$ lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/1739/$ lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/1739?(.*)$ lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/1739/?(.*)$ lordmatt.co.uk/item/2429/ [R=302,L]
RewriteCond %{HTTP_REFERER} lordmatt.co.uk [OR]
RewriteCond %{HTTP_REFERER} lordmatt.co.uk [NC]
RewriteRule .? - [S=4]
RewriteRule ^item/966$ lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/966/$ lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/966?(.*)$ lordmatt.co.uk/item/2429/ [R=302,L]
RewriteRule ^item/966/?(.*)$ 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
httpd.apache.org/docs/2.2/rewrite/access.html http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/ opensourcehacker.com/2011/09/19/http-referer-based-redirects-in-apache/
There is one simple way of doing this:
First: Create the redirects.
Redirect permanent /articles/old-article-1.html www.example.com/articles/new-article.html Redirect permanent /articles/old-article-2.html 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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.