Redirect php pages to ASP.NET pages without affecting SEO
Did you migrate your php website to ASP.NET and want to redirect all requests to .php pages to the new .aspx pages? Learn how to use http handlers to handle php pages from ASP.NET and redirect.
There are lot of websites being migrated from PHP to ASP.NET for various reasons. Explaining how to migrate from PHP to ASP.NET is beyond the scope of this article. Many websites would do a complete switchover from php to ASP.NET. In case of public websites, search engines may have indexed the .php pages and all requests to such pages will fail once you switch over to ASP.NET. If you are thinking about Search Engine optimization and search engine ranking, you will probably have to start all over again and build traffic and ranking for your new site.Impact of SEO when moving from PHP to ASP.NET
When you move from one domain name to another domain name, you can use webmaster tools of search engines to let them know about your domain name change, which will transfer your search engine ranking to the new domain. However, it is more challenging when you move from php to ASP.NET and use the same domain. All your old urls will become invalid and so is your search engine ranking.
You can retain the search engine ranking of your previous site if you do a 301 redirect from your old .php pages to corresponding .aspx pages. You must have a one to one mapping for all of your webpages from old page to new page.
To retain the search engine ranking and traffic after you move from php to ASP.NET is, use an http handler in your IIS website. This httphandler should pick up all requests to the .php pages and do a 301 redirect to the corresponding .aspx pages. By doing a 301 redirect, you are telling search engines that your pages are moved to a new location and you will retain all search engine ranking that your old pages had.HttpHandler to redirect traffic from .php to .aspx pages
Here is the sample code for the HttpHandler that can process requests to .PHP urls and redirect the traffic to corresponding .ASPX pages using 301 redirect:
using System;
using System.Web;
namespace Php2AspNet
{
public class Php2AspNetHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string originalUrl = context.Request.Url.PathAndQuery;
// Look for the .php extension
int index = originalUrl.ToLower().IndexOf(".php");
if (index >= 0)
{
// Make new url by replacing the .php extension with .aspx
string newUrl = context.Request.Url.PathAndQuery.Substring(0, index)
+ ".aspx" +
context.Request.Url.PathAndQuery.Substring(index + 4);
context.Response.Status = "301 Moved Permanently";
context.Response.AddHeader("Location", newUrl);
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Create a new C# Class library project, create a new class with the above code and compile it. Copy your DLL to the BIN folder of your ASP.NET website.
The only thing left to do is, add the following entry to your web.config file so that your website knows how to handle the requests to any files with the extension ".php"
<system.webServer>
<handlers>
<add name="Php2AspNetHandler" path="*.php" verb="*"
type="Php2AspNet.Php2AspNetHandler"
resourceType="Unspecified"
preCondition="integratedMode" />
</handlers>
</system.webServer>
The above entry says any requests to files with .php extension should be handed over to our httphandler. The http handler will check the url, replace the .php with .aspx and do a 301 redirect to the new url.
You can see I have used very simple logic in the above C# sample code. You may need to make some changes according to your specific requirements. You can conver the code easily to VB.NET if that is your favorite language.
You may use the attached HttpHandler assembly. You have to rename the file to "Php2AspNet.dll"
Is it doing Url Rewriting am i right ?