views:

412

answers:

2

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.

A: 

Do you really want to do the cityname / ID translation in the htaccess file?

The usual way would be to match Laajakaista/*.aspx and to pass the place name as a parameter to the PHP script (which would then do the translating into an ID). Wouldn't that be a better idea?

If that is an option, you would do something like this:

rewriterule ^Laajakaista/([.]+)\.aspx$ city.php?cityname=$1 [QSA]
rewriterule ^([.]+)\.aspx$ $1.php?%{QUERY_STRING} [R=301,QSA]

You can do that with or without a 301 - a 301 will change the URL visible in the browser.

Pekka
That would solve my problem number two. However, I would still be getting a lot of requests for non-existing .aspx pages, which can now be found as similarly named .php pages.Any ideas on the first problem?
Markus Ossi
@Markus Are they all in the same directory as the city names?
Pekka
@Markus if they're in the same directory, you could build a PHP front controller (e.g. index.php instead of city.php) and decide what to do in that index.php: If a file `cityname.php` exists, include that, if not, search for a city of that name, and if that fails as well, send a 404. It's not too elegant but if you need to preserve an existing structure for SEO reasons for a while, I'd say it's okay to do. As long as the rewriterule applies to aspx files only, and not to any other resources like images, it's also okay from a performance point of view.
Pekka
I would also like to tell Google that the pages can now be found at another place, thus the 301.So, I have two problems:1. Redirect all requests for .aspx-pages to similarly named .php-pages with the parameters. (City.aspx?CityID=1 -> City.php?CityID=1)2. Redirect all requests for Laajakaista/[Cityname].aspx into corresponding queries for City.php?CityID=X, where CityName matches the CityID.
Markus Ossi
@Markus for the first problem, also consider getting rid of the file extension completely or using `htm(l)`. `php` and `aspx` are meaningless in terms of content type, anyway.
Pekka
To clarify, there are no files with the name To clarify, there are no files with the name /Laajakaista/Aanekoski.aspx for example, this was just a redirect.
Markus Ossi
@Markus I updated the suggested RewriteRule. It should translate any `aspx` requests to their respective `php` counterparts, and work for the city too.
Pekka
+1  A: 

Try adding QSA to the flags, so that it's [R=301,L,QSA]. This stands for "Query String Append" and will append all of query string from the old URL to the new URL. This should solve your issue of the parameters after the filename.

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

As for the other mappings, you may want to re-think your method. Having a mapping for each city seems messy. You should probably pass the city name as a parameter to the city.php file and have that convert from name to ID. Then it's only one RewriteRule entry in the .htaccess file, rather than a redirect for each city. For example,

RewriteRule ^Laajakaista/(.*)\.aspx$ city.php?cityname=$1 [L]
Rich Adams
I can not pass into .aspx-files, because I do not have ASP.NET anymore to run the .aspx-files. I am now on a Linux hosting, with a lot of incoming links to my domain with .aspx ending. I apologize for my messy explanation of the situation.
Markus Ossi
Sorry, I meant the PHP file.
Rich Adams