ASP Code, ASP.Net Code, PHP Code, Web

How to redirect a webpage in PHP, ASP, .Net, IIS, and .htaccess

If you've moved a domain or page, you need to redirect visitors. To transfer search engine links and ranking, you should use a 301 (HTTP code) redirect. The code "301" is interpreted by browsers and search engines as "moved permanently".

Here's how to implement URL Redirection with several web technologies:

PHP


<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>

ASP


<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently" 
Response.AddHeader "Location", " http://www.new-url.com"
>

ASP .NET


<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location","http://www.new-url.com");
}
</script>

IIS

  1. In Internet Services Manager, right click on the file or folder you wish to redirect
  2. Select the radio button titled "a redirection to a URL".
  3. Enter the redirection page URI
  4. Check "The exact url entered above" and the "A permanent redirection for this resource"
  5. Click on 'Apply'

.htaccess – redirect to new domain

Create a .htaccess file in the root directory of your old website with the below code, and all your directories/pages of your old domain will be redirected to your new domain.


Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.yournewdomainurl.com/$1 [R=301,L]

* Apache Mod-Rewrite module must be enabled

.htaccess – redirect to www

Create a .htaccess file in the root directory of your old website with the below code, and all your directories/pages of your old domain will be redirected to your www.yourdomain.com.


Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.yourdomain.com/$1 [r=301,nc]

* Apache Mod-Rewrite module must be enabled

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image