简体   繁体   English

.htaccess中的Mod_rewrite-将以index.php开头的任何内容转发到____

[英]Mod_rewrite in .htaccess - forward anything that starts with index.php to ____

UPDATE: This works: 更新:这有效:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} ^[A-Z]+\ ([^\s]+)
RewriteRule (.+) /index.cfm?event=checkuri&uri=%1 [QSA]

Some background... 一些背景...

So we already have a catchall redirect in our .htaccess file which is this: 因此,我们的.htaccess文件中已经有了一个全面重定向,它是:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /index.cfm?event=checkuri&uri=$1 

This ties into a database table that checks the URI for a match. 这与检查URI是否匹配的数据库表相关联。 so if we just moved a site that used to have this page: 因此,如果我们只是移动了一个曾经拥有此页面的网站:

/some-awesome-article.html /some-awesome-article.html

Onto our system, and the new address is 进入我们的系统,新地址是

/awesome-article/12442 /真棒-文/ 12442

and someone tried to access the old URI, our system would check for this, find a match, and forward them to the new home: /awesome-article/12442 并且有人尝试访问旧的URI,我们的系统会检查该内容,找到匹配项并将其转发到新的首页:/ awesome-article / 12442


This system works awesome, with one exception. 这个系统很棒,除了一个例外。 If the URI is something like /index.php?id=123412 then the whole system falls apart. 如果URI是/index.php?id=123412之类的东西,那么整个系统就会崩溃。 In fact /index.php/whatever won't work either. 实际上,/ index.php /无论如何都不起作用。

Everything else works except for this. 除此之外,其他所有方法都起作用。 We do not use PHP for our web application (although support says its in an admin console on the server somewhere). 我们不将PHP用于我们的Web应用程序(尽管支持说它在服务器上某个地方的管理控制台中)。


So basically what I need is if index.php is detected anywhere it will forward the URI to our existing system: 所以基本上我需要的是,如果在任何地方检测到index.php,它将URI转发到我们现有的系统:

How can i modify this to fix it? 我该如何修改以解决此问题?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /index.cfm?event=checkuri&uri=$1 

Try changing your code to: 尝试将代码更改为:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /index.cfm?event=checkuri&uri=$1 [L,QSA]

QSA is for Query String Append that will make sure to append existing query parameters with the new ones. QSA用于“查询字符串追加”,可确保将现有查询参数附加到新参数中。

Based on your comments, it sounds like you need to use the Query String Append QSA flag on your rule like this: 根据您的评论,听起来您需要像这样在规则上使用“查询字符串附加QSA标志:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.cfm?event=checkuri&uri=$1 [QSA,L]

In your example case the rewrite would look like: 在您的示例情况下,重写如下所示:

/index.cfm?event=checkuri&uri=index.php&id=123412

Rewriting with mod_rewrite does not work on the full URL. 使用mod_rewrite重写不适用于完整URL。 In fact, the regex in the RewriteRule does only get the path and file, but not the query string. 实际上,RewriteRule中的正则表达式仅获取路径和文件,而不获取查询字符串。 And so the backreference $1 will only contain "index.php" and nothing else. 因此,反向引用$1将仅包含“ index.php”,而不包含其他内容。

Additionally, the RewriteRule does change the query string because there is one in the target pattern. 此外,RewriteRule确实会更改查询字符串,因为目标模式中有一个。 Because the flag [QSA] (query string append) is not present, the query string of the original request gets replaced instead of appended. 因为不存在标志[QSA] (附加查询字符串),所以将替换而不是附加原始请求的查询字符串。 So the query string is gone after this rewriting. 因此,查询字符串在此重写之后就消失了。

This would be a lot easier if you wouldn't mess with the query string. 如果您不会弄乱查询字符串,这会容易得多。 The easiest way of rewriting any url that is not an existing file would be if the second line would be simply RewriteRule (.+) /index.cfm - you could then get all info about the current request, including query string, path and file, in the script. 如果第二行只是RewriteRule (.+) /index.cfm ,那么重写不是现有文件的url的最简单方法是-然后可以获取有关当前请求的所有信息,包括查询字符串,路径和文件,在脚本中。

So now you'd have to fiddle with the query string. 因此,现在您必须摆弄查询字符串。 Adding [QSA] will pass the query string to your script and you'd have to detect what's inside. 添加[QSA]会将查询字符串传递到您的脚本,您将必须检测其中的内容。 This will work only if you do not expect the query string to contain parameters named "event" and "uri" - these will be overwritten by your rewriting. 仅当您不希望查询字符串包含名为“ event”和“ uri”的参数时,此方法才有效-重写将覆盖这些参数。 If you need to add the original query string to the URL, it's a bit more complicated, because the string needs to be url-encoded. 如果您需要将原始查询字符串添加到URL,则有点复杂,因为该字符串需要进行url编码。

Here's how to do that. 这是这样做的方法。

Sven was very close so I'm giving him the check 斯文很近,所以我给他支票

This ended up working perfectly: 最终效果完美:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} ^[A-Z]+\ ([^\s]+)
RewriteRule (.+) /index.cfm?event=checkuri&uri=%1 [QSA]

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

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