2013-12-12 04:24
浏览 45

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:


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?



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

图片转代码服务由CSDN问答提供 功能建议

在我的项目的根文件夹中,还有其他几个文件夹,我称之为“apps”。 其中一个应用程序的路径如下:

/ root_folder / myapp /...

实际上有十几个这样的应用程序。 所以我希望我的项目使用友好的URL,并实现我使用apache模块 mod_rewrite

我将以下规则添加到我的.htaccess(在root_folder中,只是为了让您知道) RewriteRule([^ /] *)/(。*)?$ myDomain.com/$1/index.php?params=$2 [NC,L]

因此,诸如mydomain / myApp / param / value / param2 / value等URL 被翻译成mydomain / myApp / index.php?params = value / param2 / value

我执行了一些测试并看到它工作,直到我添加$ 1来引用app文件夹(有 一看:the_path_to_my_root_folder_here / $ 1 /index.php?params=$2)

它生成的网址如下:myDomain.com/myApp/index.php? params = index.php

我认为这将是一个递归问题。 所以似乎Apache会在第一次执行时尝试另一个重定向,然后它会生成一个类似的URL

我在Stackoverflow中找到了这个线程 以递归方式重写 答案的问题在于它假设我知道何时停止。




我现在正在尝试以下规则 RewriteRule myDomain.com/([A-Za-z0-9-] +)/(.*)$/myDomain.com/$1/index.php? params = $ 2 [NC,L]

但它无法正常工作。 它只匹配正则表达式,如果我没有在应用程序名称后面传递参数。 因此,当我尝试myDomain.com/user/时,它可以工作(不接收参数),例如,当我尝试myDomain.com/user/products/1000/时失败。 它没有重写/重定向,而是试图在用户等内找到产品文件夹

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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
    解决 无用
    打赏 举报

相关推荐 更多相似问题