dqy13020940 2013-12-12 04:24
浏览 50
已采纳

htaccess URL重写中的递归重定向(Apache中的mod_rewrite)

In the root folder of my project there are several other folders I am calling "apps". The path to one of these apps is something like:

/root_folder/myapp/...

There are actually a dozen apps like this. So I want my project to be using friendly URLs, and to achieve that I am using the apache module mod_rewrite.

I added the following rule to my .htaccess (that is in root_folder, just so you to know) RewriteRule ([^/]*)/(.*)?$ myDomain.com/$1/index.php?params=$2 [NC,L]

So an URL such as mydomain/myApp/param/value/param2/value would be translated to mydomain/myApp/index.php?params=value/param2/value

I performed some tests and saw it working until I added the $1 to refer to the app folder (have a look: the_path_to_my_root_folder_here/$1/index.php?params=$2)

It is generating an URL like: myDomain.com/myApp/index.php?params=index.php

Well I thought it would be a recursion issue. So it seems that Apache will try another redirection afther the first is performed, and then it will generate an URL like that

I found this thread in Stackoverflow url rewrite recursively The problem with the answer is that it`s assuming I know when to stop.

Do you know how to make this second redirection to stop?

Thanks

@EDIT

I am trying the following rule now RewriteRule myDomain.com/([A-Za-z0-9-]+)/(.*)$/ myDomain.com/$1/index.php?params=$2 [NC,L]

But it's not working properly. It is only matching the regex, if I do not pass parameters after the app name. so when I try myDomain.com/user/ it works (not receiving parameters), and fails when I try myDomain.com/user/products/1000/, for example. Instead of rewriting/redirecting, it is trying to find a folder products inside user and etc

  • 写回答

2条回答 默认 最新

  • douzhan1238 2013-12-12 05:31
    关注

    Try changing your rewrite rule to something that will match the input url but not your output url. Something like this:

    RewriteRule ^([^\/]+)\/([a-z]+)\/(.+)$ /$1/index.php?params=$2/$3
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于温度改变石墨烯介电性能(关键词-介电常数)
  • ¥150 HDMI分路器LT86102 的输出在890MHz频点处EMC超标8DB
  • ¥15 druid(相关搜索:数据库|防火墙)
  • ¥15 大一python作业
  • ¥15 preLaunchTask"C/C++: aarch64- apple-darwin22-g++-14 生成活动 文件”已终止,退出代码为-1。
  • ¥60 如何鉴定微信小程序数据被篡改过
  • ¥18 关于#贝叶斯概率#的问题:这篇文章中利用em算法求出了对数似然值作为概率表参数,然后进行概率表计算,这个概率表是怎样计算的呀
  • ¥20 C#上传XML格式数据
  • ¥15 elementui上传结合oss接口断点续传,现在只差停止上传和继续上传,各大精英看下
  • ¥20 手机截图相片分辨率降低一半