【问题标题】:301 redirect for old pages of the website with codeIgniter使用 codeIgniter 对网站的旧页面进行 301 重定向
【发布时间】:2012-09-05 14:56:26
【问题描述】:

我刚刚将我的一个静态网站升级为启用了 codeIgniter 的网站。我需要将旧网站的确切域名重定向到新网站以避免 404 错误。

  • 旧页> http://example.com/pagename.php
  • 新页面 > http://example.com/index.php/home/category_images/9(cat_id)

我没有很高的没有。的页面如此硬编码所有页面直接不会是一个问题。我试过了:

RewriteEngine On
RewriteBase /
RewriteRule ^http://www.example.com/pagename.php$ http://example.com/index.php/home/category_images/9 [L,R=301]

不工作,我不知道为什么。 mod_rewrite 在我的 apache 服务器上启用。

另外,为了确认,还请告诉我应该将该文件放在哪个文件夹中,我在根目录或应用程序文件夹之间感到困惑。

谢谢。

第 2 版:现在根据 Michael 先生的建议,我尝试使用如下所示的重定向功能来实现它:

默认控制器函数:home.php

function __construct()
{
    // Call the Model constructor
    parent::__construct();
    $this->load->model('common_model');
    $this->handle_redirects();
}

function handle_redirects () 
{
    $redirects = Array(
    'gorgeous-girls.php' => 'home/category_images/9'
    );
    $uri = implode('/', $this->uri->segments);
    foreach ($redirects as $from => $to)
    {
        $from = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $from));
        if (preg_match("#^{$from}$#i", $uri))
        {
        if (strpos($to, '$') !== false and strpos($from, '(') !== false)
            $to = preg_replace("#^{$from}$#i", $to, $uri);
        redirect($to , 'location', 301);
        }
    }
}

我的 .htaccess 看起来像:

RewriteEngine On
RewriteBase /
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

PS : 我已经从我的域名结构中删除了 index.php。它看起来像:www.example.com/controller/function/uri/uri

现在我没有从主机收到错误页面,但我从 CodeIgniter 收到了错误页面。

请帮忙。

【问题讨论】:

    标签: php .htaccess codeigniter


    【解决方案1】:

    考虑到您的超文本访问文件应该通过 index.php 路由所有内容,最好使用 CodeIgniter 的 redirect() 函数处理您的重定向。

    下面提供了一个示例,基于我在 Page 控制器中的内容,在我自己的框架扩展中(它可以在 redirect() 函数的任何地方使用可用,以及 $this->uri->segments。

    function handle_redirects () {
        $redirects = Array(
            'pagename.php' => 'home/category_images/9'
        );
        $uri = implode('/', $this->uri->segments);
        foreach ($redirects as $from => $to)
        {
            $from = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $from));
            if (preg_match("#^{$from}$#i", $uri))
            {
                if (strpos($to, '$') !== false and strpos($from, '(') !== false)
                    $to = preg_replace("#^{$from}$#i", $to, $uri);
                redirect($to , 'location', 301);
            }
        }
    }
    

    如果您要将查询传递给框架中的 pagename.php 文件,您可以考虑以下内容(请注意,我不知道您的页面的意图是什么 - 这是一个通用解决方案):

    $redirects = Array(
        'pagename.php?q=(:any)' => 'home/category_images/$1'
    );
    

    例如:

    http://example.com/pagename.php?q=9
    

    会将您重定向到:

    http://example.com/home/category_images/9
    

    这个解决方案非常适合我,并且在跨浏览器兼容性方面非常有效。

    请注意,您的 RewriteRules 应如下所示,或类似:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule (xml|txt|css|js|jpg|png|gif)$ - [L]
    RewriteRule .* index.php [L,QSA]
    

    编辑:以上内容已更改以适应资产。基本上,它告诉 Apache 通过index.php 路由所有内容,除非它是一个存在且具有指定扩展名的文件。 (这是出于安全目的的良好做法。)

    编辑:请注意,您不应在 URL 中包含 index.php,因为无论如何它都会被 .htaccess 文件剥离。

    请注意,我使用的是 PHP 5.3 - 尽管此解决方案应该适合您。如果有任何问题,请告诉我。

    【讨论】:

    • 先生,我对 codeIgniter 甚至 PHP 都很陌生,我还没有从我的域名中删除 index.php,这是否意味着一切都通过它路由?
    • 另外,请告诉我如何使用它,home.php 是我的默认控制器,所以我应该在 home.php 的任何地方使用此代码创建另一个函数,还是我需要做任何具体的事情。另外,我该如何调用这个函数?我的用例是,我要重定向的域是我旧网站的静态页面,它有很好的 seo,每天都有很多人访问它们,我不需要向旧页面传递任何查询。
    • @Vinay:出于 SEO 目的,所有内容都应通过 index.php 进行路由。如果你的重写规则看起来和我发布的一样,那么把这个函数放在你的家庭控制器中。然后,在控制器构造函数中,添加 $this->handle_redirects()。一旦函数工作,您可以简单地将规则添加到 $redirects 数组。通过使用这些重定向,您善意地告诉搜索引擎您已经更改了 URI 结构,并且您在这些搜索引擎上的状态应该保持不变。如果这对你有用,请告诉我。 :)
    • 先生,我完全按照您的要求做了,但它不起作用。通过谷歌搜索这个问题,我已经尝试了几乎所有可以在不同博客上找到的东西。它没有得到解决! :( :(
    • @Vinay:也许如果你分享我提出的答案的实现(通过编辑你的问题——也许其他人可能知道基于 obn 我的更好的解决方案),并告诉我它是否抛出任何错误在你。
    【解决方案2】:

    您没有在重写规则的匹配部分使用整个域名。试试这个:

    RewriteRule ^pagename.php$ /index.php/home/category_images/9 [L,R=301]
    

    当然,您可能需要其他规则让服务器了解 index.php 是要调用的文件,而不是 /image.php/home/category_images/9 中的文件

    所以也许第二条规则像

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^index.php/(.*) /index.php?q=$1 [L]
    

    我刚刚注意到,在您更新的答案中,您试图从 www.example.com 重定向到 example.com。如果您确实想强制使用一个域,您可能应该在 .htaccess 中添加另一条规则,然后再提及如下任何其他规则:

    RewriteCond %{HTTP_HOST} ^www.example.com$
    Rewrite Rule ^(.*)$ http://example.com/$1 [L,R=301]
    

    【讨论】:

    • 这就是我的代码现在在 htaccess 字段中的样子,但它仍然无法正常工作? :( RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www.example.com$ 重写规则 ^(.*)$ example.com/$1 [L,R=301] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} ! -f RewriteRule ^index.php/(.*) /index.php?q=$1 [L] RewriteRule ^page-name.php$ /index.php/home/category_images/9 [L,R=301] 而我已将我的 .htaccess 文件放在 Application 文件夹中。有什么想法吗?
    • @VinaySinghal .htaccess 规则根本不起作用还是只是一些规则?我问的原因是您可能需要将主 apache conf 文件设置为 AllowOverride。没有这个,.htaccess 文件就会被忽略。
    • 先生,我对开发很陌生。我对此一无所知。我不知道哪个有效,哪个无效。如果你问我,我可以进行测试。此外,该网站托管在 1and1.com 的共享托管帐户上
    • @VinaySinghal 试试这个。在您的 Web 根目录中的 .htaccess 文件的开头键入非法行。像BadDirectiveHere 这样的东西。然后尝试请求服务器上的任何页面。你应该得到一个 500 错误。如果你不这样做,那么我们就会知道你没有打开AllowOverride。
    • 我收到 500 错误,先生。当我这样做时,我收到一个错误,我无法访问我的网站。 @迈克布兰特
    【解决方案3】:

    你可以打开你的xampp/apache/conf/httpd.conf

    然后检查下面的行

    #LoadModule rewrite_module modules/mod_rewrite.so
    

    如果哈希 (#) 存在于该行的上方。然后删除它。

    LoadModule rewrite_module modules/mod_rewrite.so
    

    并重新启动您的 apache 服务器并检查您的站点。

    然后网站无法正常工作,请告诉我。

    【讨论】:

    • 先生,mod_rewrite 已启用且未在此文件中注释,并且我托管它的网站服务器也已启用。仍然无法正常工作。现在我正在尝试: - RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www.example.com$ Rewrite Rule ^(.*)$ example.com/$1 [L,R=301] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^index.php/(.*) /index.php?q=$1 [L] RewriteRule ^page-name.php$ /index.php/home/category_images/9 [L, R=301] 这也不起作用,先生。
    猜你喜欢
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    相关资源
    最近更新 更多