简体   繁体   English

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

[英]Recursive redirection in htaccess URL rewriting (mod_rewrite in Apache)

In the root folder of my project there are several other folders I am calling "apps".在我的项目的根文件夹中,还有其他几个文件夹,我称之为“apps”。 The path to one of these apps is something like: /root_folder/myapp/...这些应用程序之一的路径类似于: /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 .所以我希望我的项目使用友好的 URL,并实现我使用 apache 模块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]我将以下规则添加到我的 .htaccess(在 root_folder 中,只是为了让您知道) 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因此,诸如 mydomain/myApp/param/value/param2/value 之类的 URL 将被转换为 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) The path to one of these apps is something like: `/root_folder/myapp/...** It is generating an URL like: myDomain.com/myApp/index.php?params=index.php我执行了一些测试并看到它工作,直到我添加 $1 来引用应用程序文件夹(看看:the_path_to_my_root_folder_here/ $1 /index.php?params=$2)这些应用程序之一的路径类似于:`/root_folder /myapp/...** 它正在生成一个 URL,如: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 after the first is performed, and then it will generate an URL like that所以看起来Apache会在第一次执行后尝试另一个重定向,然后它会生成一个这样的URL

I found this thread in Stack Overflow The problem with the answer is that it`s assuming I know when to stop.我在 Stack Overflow 中找到了这个线程答案的问题是它假设我知道什么时候停止。

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.我现在正在尝试以下规则RewriteRule myDomain.com/([A-Za-z0-9-]+)/(.*)$/ myDomain.com/$1/index.php?params=$2 [NC,L] ,但它不能正常工作。 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.因此,当我尝试 myDomain.com/user/ 时,它可以工作(不接收参数),并且在我尝试 myDomain.com/user/products/1000/ 时失败。 Instead of rewriting/redirecting, it is trying to find a folder products inside user and etc它不是重写/重定向,而是尝试在用户等内部找到文件夹产品

Try changing your rewrite rule to something that will match the input url but not your output url.尝试将重写规则更改为与输入 url 匹配但与输出 url 不匹配的内容。 Something like this:像这样的东西:

RewriteRule ^([^\/]+)\/([a-z]+)\/(.+)$ /$1/index.php?params=$2/$3

Place this rule in DOCUMET_ROOT/.htaccess :将此规则放在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无法理解the_path_to_my_root_folder_here意思

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM