简体   繁体   中英

Htaccess rewrite rule .aspx to .php

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.

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM