{"id":92,"date":"2008-09-23T17:04:03","date_gmt":"2008-09-23T21:04:03","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2008\/09\/23\/performing-301-redirect-with-apache-iis-php-and-others\/"},"modified":"2008-09-23T17:04:03","modified_gmt":"2008-09-23T21:04:03","slug":"performing-301-redirect-with-apache-iis-php-and-others","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2008\/09\/23\/performing-301-redirect-with-apache-iis-php-and-others\/","title":{"rendered":"Performing 301 Redirect with Apache, IIS, PHP, and others"},"content":{"rendered":"<p><b>Apache<\/b><\/p>\n<p>If you use Apache with the mod-rewrite module installed, 301 Redirect may be set up by editing your .htaccess file found at the root directory of your website.  The following is a sample of what you might add to your .htaccess file; in this example, we are redirecting any visits to \/blah\/pictures.html to its new location, <a href=\"http:\/\/ww2db.com\/photo.php\" target=\"_ww2db\">http:\/\/ww2db.com\/photo.php<\/a>.<\/p>\n<p class=\"code\">\nRedirect 301 \/blah\/pictures.html http:\/\/ww2db.com\/photo.php\n<\/p>\n<p>Sometimes you may encounter a situation where you changed technology of your platform, for example, from static HTML pages to dynamic PHP pages.  With the sample code below, you can redirect all HTML pages to PHP pages by the same name.<\/p>\n<pre class=\"code\">\nRedirectMatch 301 (.*).html$ http:\/\/ww2db.com$1.php\n<\/pre>\n<p>Looking to use .htaccess to redirect an entire site because you have moved from one domain to another?  This is what you want to do:<\/p>\n<pre class=\"code\">\nRedirect 301 \/ http:\/\/ww2db.com\/\n<\/pre>\n<p><b>PHP<\/b><\/p>\n<p>If your site&#8217;s pages are made of PHP files, redirection is fairly easy.  If you had moved the \/old\/pictures.php page to the new location of <a href=\"http:\/\/ww2db.com\/photo.php\" target=\"_ww2db\">http:\/\/ww2db.com\/photo.php<\/a>, modify the \/old\/pictures.php page by adding the following PHP snipped on the top of the code, ie. before any HTML code is written to screen.<\/p>\n<pre class=\"code\">\nheader(\"HTTP\/1.1 301 Moved Permanently\");\nheader(\"Location: http:\/\/ww2db.com\/photo.php\");\n<\/pre>\n<p><b>IIS<\/b><\/p>\n<p>If you using Microsoft&#8217;s Internet Information Services (IIS), perform the following steps to do a 301 Redirect.<\/p>\n<ul>\n<li>Right click on &#8220;My Computer&#8221;, go to &#8220;Manage&#8221;, &#8220;Services and Applications&#8221;, and then &#8220;Internet Information Services (IIS) Manager&#8221;.<\/li>\n<li>Under &#8220;Web Sites&#8221;, look for the particular folder or file you wish to redirect.  Right click and select &#8220;Properties&#8221;.<\/li>\n<li>Click the &#8220;A redirection to a URL&#8221; radio button.  In the bottom section that had just appeared, and enter the new URL in the &#8220;Redirect to:&#8221; text box, and make sure to check &#8220;A permanent redirection for this resource&#8221; checkbox.<\/li>\n<li>Click OK to complete the process.<\/li>\n<\/ul>\n<p><b>ASP and ASP.NET<\/b><\/p>\n<p>Classic Active Server Pages (ASP) web pages can be redirected in a method very similar to PHP above.<\/p>\n<pre class=\"code\">\nResponse.Status=\"301 Moved Permanently\"\nResponse.AddHeader \"Location\",\"http:\/\/ww2db.com\/photo.php\"\n<\/pre>\n<p>ASP.NET redirect code is very similar to its ASP predecessor&#8217;s.<\/p>\n<pre class=\"code\">\nprivate void Page_Load(object sender, System.EventArgs e) {\n  Response.Status=\"301 Moved Permanently\";\n  Response.AddHeader(\"Location\",\"http:\/\/ww2db.com\/photo.php\");\n}\n<\/pre>\n<p><b>JSP<\/b><\/p>\n<p>Redirection for Java Server Pages (JSP), again, is similar to the other scripting languages we have seen above.<\/p>\n<pre class=\"code\">\nresponse.setStatus(301);\nresponse.setHeader(\"Location\",\"http:\/\/ww2db.com\/photo.php\");\nresponse.setHeader(\"Connection\", \"close\");\n<\/pre>\n<p><b>CGI\/Perl<\/b><\/p>\n<p>If you are using Perl-based CGI code, this is the code you may wish to deploy to perform a 301 Redirect.<\/p>\n<pre class=\"code\">\n$q=new CGI;\nprint $q->redirect(\"http:\/\/ww2db.com\/photo.php\");\n<\/pre>\n<p><b>Cold Fusion<\/b><\/p>\n<p>ColdFusion pages can be redirect also by working with the header.<\/p>\n<pre class=\"code\">\n&lt;.cfheader statuscode=\"301\" statustext=\"Moved permanently\"&gt;\n&lt;.cfheader name=\"Location\" value=\"http:\/\/ww2db.com\/photo.php\"&gt;\n<\/pre>\n<p><b>Ruby on Rails<\/b><\/p>\n<p>Not surprisingly, Ruby on Rails pages are redirected in a very similar manner.<\/p>\n<pre class=\"code\">\ndef old_action\nheaders[\"Status\"]=\"301 Moved Permanently\"\nredirect_to \"http:\/\/ww2db.com\/photo.php\"\nend\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When you change the URL of a page or move an entire site to another URL, all those sites that link to you may get broken links, and if you care about SEO results, all the search engine &#8220;points&#8221; you have been building over the years might be all lost.  By employing the 301 Redirect technique, you might be able to avoid these problems completely.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,5,6,12,29,7,8,4,41,18],"tags":[],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-apache","category-asp","category-asp-net","category-coldfusion","category-iis","category-jsp","category-perl","category-php","category-ruby-on-rails","category-seo"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}