dsf487787 2012-09-05 14:56 采纳率: 100%
浏览 68

301使用codeIgniter重定向网站的旧页面

I have just now upgraded one of my static website to a codeIgniter enabled website. I need to redirect the exact domain names of the old website to the new ones to avoid the 404 error.

  • Old pages > http://example.com/pagename.php
  • New pages > http://example.com/index.php/home/category_images/9(cat_id)

I do not a very high no. of pages so hard coding all the pages directly will not be a problem. I have tried :

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

Not working, I have no idea why. mod_rewrite is enable on my apache server.

Also, just for the confirmation please also tell me in which folder I should put this file, I am confused between root dir or Application folder.

Thank you.

Version 2 : Now after suggestion from Mr. Michael, I tried to implement it using the redirect function like this :

Default Controller function : 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);
        }
    }
}

And my .htaccess looks like :

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

PS : I have removed index.php from my domain name structure. It looks like : www.example.com/controller/function/uri/uri

Now I am not getting an error page from my host, but I am getting an error page from the CodeIgniter.

Please help.

  • 写回答

3条回答 默认 最新

  • drugs3550 2012-09-05 15:02
    关注

    You don't use the whole domain name in the match portion of the rewrite rule. Try this:

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

    Of course you will likely need other rules for the server to understand that index.php is the file that is to be called, not something at /image.php/home/category_images/9

    So perhaps a second rule like

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

    I just noticed that in your updated answer you are trying to redirect from www.example.com to example.com. If you do want to force one domain, you should probably add another rule in your .htaccess before either of the other rules mentioned like this:

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法