简体   繁体   English

htaccess将规则.aspx重写为.php

[英]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. 背景:我有一个使用ASP.NET 2.0构建的网站,并且该网站位于Windows托管上。 I now have to rewrite my site in PHP and move it into Linux hosting. 现在,我必须用PHP重写网站并将其移至Linux托管中。 I have a lot of incoming links to my site from around the web that point directly into the old .aspx-pages. 我有很多从网站到网站的链接,这些链接直接指向旧的.aspx页。 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. 我将静态.aspx页另存为.php-pages,并用PHP重写了动态页。 The dynamic page is called City.aspx and I have written it in PHP and it is now called City.php. 动态页面称为City.aspx,我已经用PHP编写了它,现在称为City.php。

On my old Windows hosting, I used ASP.NET's URL mapping for friendly URL. 在旧的Windows主机上,我使用ASP.NET的URL映射作为友好的URL。 For example, incoming URL request for Laajakaista/Ypaja.aspx was mapped into City.aspx?CityID=981. 例如,对于Laajakaista / Ypaja.aspx的传入URL请求已映射到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. 我的目标:将所有寻找旧.aspx页面的访客和搜索引擎重定向到新的.php页面。

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. 我认为将访问者重定向到新页面的最简单方法是进行重定向,将所有对.aspx文件的请求都重定向到.php文件类型。

So, if someone asks for MYSITE/City.aspx?CityID=5, they will be taken into MYSITE/City.php?CityID=5 instead. 因此,如果有人要求MYSITE / City.aspx?CityID = 5,他们将被带入MYSITE / City.php?CityID = 5。

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: 为了使事情变得更加复杂,在我以前的站点中,我使用了友好的URL,因此我拥有一个巨大的映射文件,其中包含如下所示的映射:

    <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... 我最终遇到内部服务器错误和一个50k的.htaccess文件...

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Do you really want to do the cityname / ID translation in the htaccess file? 您是否真的要在htaccess文件中进行城市名称/ ID转换?

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). 通常的方法是匹配Laajakaista/*.aspx并将地名作为参数传递给PHP脚本(然后将其转换为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. 不论是否使用301,您都可以这样做-301会更改浏览器中可见的URL。

Try adding QSA to the flags, so that it's [R=301,L,QSA] . 尝试将QSA添加到标志中,使其为[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. 这代表“查询字符串追加”,并将所有查询字符串从旧URL追加到新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. 您可能应该将城市名称作为参数传递给city.php文件,并将其从名称转换为ID。 Then it's only one RewriteRule entry in the .htaccess file, rather than a redirect for each city. 然后,它只是.htaccess文件中的一个RewriteRule条目,而不是每个城市的重定向。 For example, 例如,

RewriteRule ^Laajakaista/(.*)\.aspx$ city.php?cityname=$1 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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