Mod_rewrite - how to 301 redirect an old URL to a new one

只愿长相守 提交于 2019-12-02 18:48:36

问题


I need to grab some of my website's old URLs and do a 301 redirect to the new ones, since they are already indexed and we don't want to loose relevance after the change. The old URL is in fact very ugly and for some reason everything I try to do to rewrite it does not work. Here it is:

http://www.mywebsite.com/ExibeCurso.asp?Comando=TreinamentoGeral&codCurso=136&Titulo=Como%20Estruturar%20um%20Sistema%20Gerencial%20de%20Controles%20Organizacionais,13

Basically, I need to translate it into something like:

http://www.mywebsite.com/curso/136

From the old URL I need to check if the user typed "ExibeCurso.asp"; then I know I must send him here: /curso. I must also grab the integer that was in the querystring parameter "codCurso" (136). What is the regular expression I must use for this. I am using ISAPI_Rewrite 3, which basically implements htaccess on IIS, so there should be no difference in terms of syntax. Thanks.


回答1:


Try this rule:

RewriteCond %{QUERY_STRING} ^([^&]*&)*codCurso=([0-9]+)(&.*)?$
RewriteRule ^/ExibeCurso\.asp$ /curso/%2? [L,R=301]

But I’m not sure whether ISAPI Rewrite requires the pattern to begin with a slash.




回答2:


Off the top of my head, something like this should work:

RewriteRule ^ExibeCurso.asp(.*)$ http://www.mywebsite.com/curso/$1 [L,R=301]

That would at least send the traffic to /curso/ with all parameters attached. Maybe it's best to process it from there.



来源:https://stackoverflow.com/questions/1112801/mod-rewrite-how-to-301-redirect-an-old-url-to-a-new-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!