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

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

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

/ root_folder / myapp /... strong> p>

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

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

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

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

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

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

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

你知道如何让第二次重定向停止吗? p>

谢谢 p>

@EDIT p>

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

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

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

Place this rule in DOCUMET_ROOT/.htaccess:

RewriteEngine On

RewriteRule ^([^/]+)/(.+)$ /$1/index.php?params=$2 [L,QSA]

Unable to understand what you mean by the_path_to_my_root_folder_here