Htaccess rewrite rule .aspx to .php

Posted by Markus Ossi on Stack Overflow See other posts from Stack Overflow or by Markus Ossi
Published on 2010-04-28T14:44:01Z Indexed on 2010/04/28 15:23 UTC
Read the original article Hit count: 428

Background: I have a website that has been built with ASP.NET 2.0 and is on Windows hosting. I now have to rewrite my site in PHP and move it into Linux hosting. I have a lot of incoming links to my site from around the web that point directly into the old .aspx-pages. The site itself is very simple, one dynamic page and five static ones.

I saved the static .aspx pages as .php-pages and rewrote the dynamic page in PHP. The dynamic page is called City.aspx and I have written it in PHP and it is now called City.php.

On my old Windows hosting, I used ASP.NET's URL mapping for friendly URL. For example, incoming URL request for Laajakaista/Ypaja.aspx was mapped into City.aspx?CityID=981.

My goal: To redirect all human visitors and search engines looking for the old .aspx pages into the new .php pages.

I am thinking that the easiest way to redirect visitors into new pages will be by making a redirect, where all requests for .aspx-files will be redirected into .php filetypes.

So, if someone asks for MYSITE/City.aspx?CityID=5, they will be taken into MYSITE/City.php?CityID=5 instead.

However, I am having a lot of trouble getting this to work.

So far this is what I have found:

rewriterule ^([.]+)\.aspx$ http://www.example.com/$1.php [R=301,L] 

However, I think this can not handle the parameters after the filetype and I am also not quite sure what to put on front.

To make things a bit more complicated, at my previous site I used friendly URL's so that I had a huge mapping file with mappings like this:

    <add url="~/Laajakaista/Ypaja.aspx" mappedUrl="~/City.aspx?CityID=981" />
    <add url="~/Laajakaista/Aetsa.aspx" mappedUrl="~/City.aspx?CityID=988" />
    <add url="~/Laajakaista/Ahtari.aspx" mappedUrl="~/City.aspx?CityID=989" />
    <add url="~/Laajakaista/Aanekoski.aspx" mappedUrl="~/City.aspx?CityID=992" />

I tried to make a simple redirect like this:

Redirect 301 Laajakaista/Aanekoski.aspx City.php?CityID=992

but was not able to get it to work. I ended up with an internal server error and a 50k .htaccess-file...

Any help is greatly appreciated.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about friendly-url