【问题标题】:Send 404 when requesting index.php through .htaccess?通过.htaccess 请求index.php 时发送404?
【发布时间】:2010-05-08 19:23:08
【问题描述】:

我最近重构了一个现有的 CodeIgniter 应用程序以使用 url 段而不是查询字符串,并且我正在使用 htaccess 中的重写器将内容重写到 index.php:

RewriteRule ^(.*)$ /index.php/$1 [L]

我现在的问题是这个网站的很多页面都被谷歌索引,并带有指向 index.php 的链接。由于我更改为使用 url 段,我不再关心这些谷歌结果,我想发送 404(不需要永久使用 301 Move,已经有足够的更改,它只需要重新抓取一切)。

直截了当:如何将请求重定向到 /index.php?无论如何重定向到 404 页面?我正在考虑重写一个不存在的文件,这会导致 apache 发送 404。这是一个可以接受的解决方案吗?重写后会是什么样子?

编辑:
目前,现有的谷歌结果只会导致以下错误:

遇到错误

您提交的 URI 不允许 字符。

我尝试了类似的方法:

RewriteRule ^index\.php?(.*)$ /no-exist.html [R=404,L]

但它导致了内部服务器错误。

编辑2:

CodeIgniter 已经发送“400”错误,这足以让我的网页从谷歌中删除吗?

【问题讨论】:

    标签: .htaccess codeigniter


    【解决方案1】:

    RewriteRule 的 R[=code] 标志只允许 code 范围为 300-400。

    不要使用重定向 R 标志 - 只是尝试重写到一个平淡无奇的页面:

    更新: 两个重定向相互并置 - 使用 RewriteConds 来避免干扰。

    完成.htaccess

    RewriteEngine on
    
    RewriteCond %{REQUEST_URI} ^/index.php.*
    RewriteCond %{QUERY_STRING} !^$
    RewriteRule ^(.*)$ /no-exist.html [L]
    
    RewriteCond %{REQUEST_URI} !^/index.php.* 
    RewriteCond %{REQUEST_URI} !^/no-exist.html.* 
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    注意/no-exist.html 实际上并不存在。假设,它会帮助你。

    【讨论】:

    • 我发现了问题:/no-exists.html 语句中的斜线 / 不应该在这里(只是 no-exists.html)。我已经确定了我的答案。经测试 - 有效。
    • 该斜线适用于我的其他重写...不用说,这仍然会导致内部服务器错误。也许这 2 个重写规则不能很好地结合在一起。
    • 是的,斜线没问题。我已经解决了使用RewriteCond 来逃避干扰的问题。另外:RewriteRule trunkates 查询字符串,这就是模式 ^index\.php?(.*)$ 不起作用的原因。
    【解决方案2】:

    有一个特殊的 HTTP 状态码 410 GONE 告诉世界删除资源: 请求的资源 /index.php 此服务器上不再可用,并且没有转发地址。请删除对该资源的所有引用。

    要发送此代码,请在重写规则中使用[G|gone] 标志:

    RewriteEngine on
    
    RewriteCond %{REQUEST_URI} ^/index.php.* 
    RewriteCond %{QUERY_STRING} !^$
    RewriteRule ^(.*)$ / [G,L]
    
    RewriteCond %{REQUEST_URI} !^/index.php.* 
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    【讨论】:

      猜你喜欢
      • 2013-09-14
      • 2013-08-25
      • 2022-08-03
      • 2017-08-17
      • 1970-01-01
      • 2018-06-21
      • 2013-01-15
      • 2011-06-10
      • 2013-04-27
      相关资源
      最近更新 更多