Redirecting from a link but allowing the link to still be visited with htaccess

Redirecting from a link but allowing the link to still be visited with htaccess - .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, 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 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?


Solution :

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.



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




Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect