如果url具有index.php/somefileOrsomedirectory,如何重定向

如果url具有index.php/somefileOrsomedirectory,如何重定向,php,regex,apache,.htaccess,redirect,Php,Regex,Apache,.htaccess,Redirect,我想做的是,如果url有www.example.com/index.php/anywhere,那么它应该将用户抛出到www.example.com/error-404 这是我的htaccess文件中的当前表达式 RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?$1 [L,QSA] R

我想做的是,如果url有www.example.com/index.php/anywhere,那么它应该将用户抛出到www.example.com/error-404 这是我的htaccess文件中的当前表达式

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [L,R=301]
Redirect 301 /online-features/key-anywhere-web-based-system http://www.example.com/online-features
Redirect 301 /home http://www.example.com/
提前谢谢。 如果我没有以正确的方式提问,请建议/编辑我的问题

编辑
我不想将/index.php丢失到/redirection。

我认为下面应该满足您的要求。点是任何字符,+表示一次或多次。 这需要正斜杠在那里

RewriteRule ^index\.php/(.+)$ error-404 [L]
编辑:谢谢@DocRoot,相应更新

www.example.com/index.php/anything

/这里的任何内容都是物理文件名之后的附加路径信息。您可以使用AcceptPathInfo指令专门禁用此功能:

AcceptPathInfo Off
在这种情况下,所有包含额外路径信息的URL将自动触发404。但是,URL仍然可以使用mod_rewrite进行路由,这可能就是这里发生的情况

您仍然需要实现datascript答案中提到的mod_rewrite重定向。或者在现有指令之前尝试以下操作:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/ /error-404 [R=302,L]
检查重定向_状态是为了避免重写循环,因为前端控制器似乎使用路径信息来路由请求

如果这是永久性的,在确认其正常工作后,将其更改为301

然而,如果这是一个适当的Apache错误文档,则更好。例如:

ErrorDocument /error-404.php

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/ - [R=404,L]

我只是想澄清一下-如果有任何目录和/或文件,你想让它重定向到404错误页面吗?@thickguru给定URL/index.php/anything-anything是额外的路径信息,它与目录或文件无关。@DocRoot如果它类似于1/index.php/string 2/index.php/directory 3/index.php/pathwhichdoesnotexists,那么它应该重定向到/error-404。理想情况下,您应该转义RewriteRule模式中的第一个点以匹配文字点。这是:RewriteRule^.*$index.php?$1[L,QSA]将重写所有不是文件或目录的index.php。在重写base/?/index.php/anything之后,您是否尝试过在上面的答案中直接插入这一行,这意味着它可以是一个目录或文件。我也尝试过这一行,但它对我没有帮助。请看看我目前的重定向规则,然后建议,这将对我有很大帮助。如果我删除我的所有重定向,它工作得很好,但是我也不想失去这些重定向。你在现有的指令之前已经包含了这个?类似的内容需要放在文件的顶部。您也不应该混合使用重定向mod_别名和RewriteRule mod_rewrite指令。将重定向转换为RewriteRule。你能通过编辑我的代码在答案中写下所有规则吗。我的意思是100%的代码,因为它只是答案的一部分。谢谢你,兄弟,我想它起作用了。让我再看看