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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料