【问题标题】:How to redirect if the url have index.php/somefileOrsomedirectory如果 url 有 index.php/somefileOrsomedirectory 如何重定向
【发布时间】:2017-01-06 15:32:28
【问题描述】:

我想要做的是如果 url 有 www.example.com/index.php/anything 那么它应该把用户扔到 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/ 的重定向。

【问题讨论】:

  • 我只是想澄清一下——所以如果有任何目录和/或文件,您希望它重定向到 404 错误页面吗?
  • @thickguru 给定 URL /index.php/anything - "anything" 是附加的路径信息,它与目录或文件无关。
  • @DocRoot 如果它类似于 1) /index.php/string 2) /index.php/directory 3) /index.php/pathwhichdoesnotexists 那么它应该重定向到 /error-404跨度>

标签: php regex apache .htaccess redirect


【解决方案1】:

www.example.com/index.php/anything

/anything 这里是附加的路径信息(在物理文件名之后)。您可以使用AcceptPathInfo 指令专门禁用此“功能”:

AcceptPathInfo Off

在这种情况下,所有包含附加路径信息的 URL 都会自动触发 404。但是,仍然可以使用 mod_rewrite 路由 URL,这可能就是这里发生的情况。

您仍然需要按照数据脚本的答案中提到的那样实现 mod_rewrite 重定向。或者在现有指令之前尝试以下操作:

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

检查REDIRECT_STATUS 是为了避免重写循环,因为您的前端控制器 似乎使用路径信息来路由请求。

如果这是永久性的,则将其更改为301,一旦您确认它工作正常。

但是,最好将其实现为适当的 Apache 错误文档。例如:

ErrorDocument /error-404.php

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

【讨论】:

  • /index.php/anything 表示可以是目录或文件。
  • 我也试过这个,但对我没有帮助。请查看我当前的重定向规则,然后提出建议,对我有很大帮助。如果我要删除所有重定向,效果很好,但我也不想放弃这些重定向。
  • 你已经在“现有指令之前”包含了这个?像这样的东西需要放在文件的顶部。您也不应该混合使用 Redirect (mod_alias) 和 RewriteRule (mod_rewrite) 指令。将Redirect 转换为RewriteRule
  • 你能通过编辑我的代码在答案中写下所有规则吗?我的意思是 100% 的代码,因为它只是答案的一部分。谢谢
  • 等等,兄弟,我认为它正在工作。让我再看看
【解决方案2】:

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

RewriteRule ^index\.php/(.+)$ error-404 [L]

编辑:感谢@DocRoot,已相应更新

【讨论】:

  • 理想情况下,您应该转义 RewriteRule 模式 中的第一个点以匹配文字点。
  • This: RewriteRule ^(.*)$ index.php?$1 [L,QSA] 会将所有非文件或目录的内容重写为 index.php。您是否尝试在 RewriteBase / 之后直接在上面的答案中添加该行?
猜你喜欢
  • 2020-07-07
  • 1970-01-01
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多