【问题标题】:Redirect index.php in CodeIgniter在 CodeIgniter 中重定向 index.php
【发布时间】:2011-02-21 03:27:03
【问题描述】:

我创建了一个 CodeIgniter 应用程序,现在我正在尝试将带有 index.php 的 url 重定向到没有它的 url。

我当前的 .htaccess 是:

RewriteEngine On
RewriteBase /

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enforce www
# If you have subdomains, you can add them to 
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
RewriteRule ^(.*)$ http://www.plugb.com/$1 [L,R=301]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

唯一的问题是无论有没有 index.php 都可以访问同一个页面。

例如:

http://www.plugb.com/index.php/games/adventure-rpg

http://www.plugb.com/games/adventure-rpg

有没有办法重定向 index.php 网址?

谢谢你, 加布里埃尔。

【问题讨论】:

  • 我没明白。您是否尝试从 URL 中删除 index.php?

标签: php .htaccess codeigniter


【解决方案1】:

CodeIgniter wiki documentation 描述了如何实现这一精确行为。

本文介绍了如何从 CI 应用程序 URL 中删除“index.php”。然而,它并没有消除对 Index.php 的需要,它是 CI 前端控制器,即即使 Index.php 不会出现在 URL 中,它仍然需要出现在您网站的顶层(在 /system 之上/目录)。

【讨论】:

  • 我也试过了,没有任何改变。两个 URL 都可以访问。我想让 index.php 的 URL 根本无法访问(重定向)。
【解决方案2】:

在最后一条规则之前添加:

RewriteBase /    
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^index.php(/.*)?$ http://%{HTTP_HOST}$1 [R=301]

或拒绝访问:

RewriteRule ^index.php(/.*)?$ - [R=404]

【讨论】:

  • 我试过了,没有任何改变(有和没有 index.php 的作品)。
  • 是的,id 可以,但它应该转发到没有 index.php 的位置。顺便说一句,由于您使用的是 RewriteBse /,因此重写规则应该不同。我已经更新了答案以反映这一点
  • 使用您的第一个解决方案,我得到: --- Moved Permanently 文档已移至此处。此外,在尝试使用 ErrorDocument 处理请求时遇到 403 Forbidden 错误。 --- 链接被重定向,但内容不起作用。上面的“这里”一词有一个指向同一页面的链接。 --- 我将 [R=301] 更改为 [L,R=301] (只是尝试解决方案)。它也没有工作。我有一个重定向循环。 --- 我要感谢您到现在为止的帮助。如果您能继续帮助我,我将不胜感激。
  • 发布你的 apache 日志和你的 rewritelog,rewriteloglevel 9。
  • 只有相关部分——与对 index.php 的请求有关的条目 这是访问日志,还包括重写日志。见httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteloghttpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteloglevel
【解决方案3】:

你可以试试这个吗? RewriteRule ^(.*)$ index.php/?$1 [L]

希望它会起作用。

【讨论】:

    【解决方案4】:

    我想出了一个解决方法:

    $clean_url = str_replace('index.php','',current_url());
    $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    
    if($clean_url!==$current_url)
    {
        redirect($clean_url,'location',301);
    }
    

    它不是 .htaccess,但在你的控制器中可以解决这个问题。 (至少,它在PlugB 有效)

    【讨论】:

      【解决方案5】:

      您不需要编写代码来“重定向”index.php URL。

      进入 application/config/config.php 并进行以下更改

      $config['index_page'] = "";  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-02
        • 2020-05-26
        • 2022-11-14
        • 2013-05-20
        • 1970-01-01
        • 2019-11-23
        • 2012-03-18
        • 2018-08-06
        相关资源
        最近更新 更多