301重定向与codeIgniter旧网页的网站 [英] 301 redirect for old pages of the website with codeIgniter

查看:125
本文介绍了301重定向与codeIgniter旧网页的网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级现在我的静态网站到codeIgniter启用的网站之一。我需要把旧网站的准确域名重定向到新的,以避免404错误。

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

我不很高的没有。的网页,以便硬编码的所有页直接将不会是一个问题。我曾尝试:

  RewriteEngine叙述上
的RewriteBase /
重写规则^的http://www.example.com/pagename.php$ http://example.com/index.php/home/category_images/9 [L,R = 301]
 

不工作,我不知道为什么。 的mod_rewrite 是让我的Apache服务器上。

此外,只为确认也请告诉我在哪个文件夹我应该把这个文件,我感到困惑的根目录或应用程序文件夹之间。

感谢你。

2版:现在从迈克尔先生的建议后,我试图用实现像​​这样的重定向功能:

默认的控制器功能:home.php

 函数__construct()
{
    //调用模型构造
    父:: __结构();
    $这个 - >负载>模型('common_model');
    $这个 - > handle_redirects();
}

功能handle_redirects()
{
    $重定向=阵列(
    华丽-girls.php'=>家/ category_images / 9'
    );
    $ URI =破灭('/',$这个 - > URI的>段);
    的foreach($重定向为$从=> $到)
    {
        从$ = str_replace函数(':任何,str_replace函数('+'':NUM','[0-9] +',从$));
        如果(preg_match(#^ {$从} $#我,$ URI))
        {
        如果(strpos($到,'$')!== false并且strpos($的,'(')!= FALSE)
            $为= preg_replace(#^ {$从} $#我,$到,$ URI);
        重定向($到,位置,301);
        }
    }
}
 

和我的.htaccess如下:

  RewriteEngine叙述上
的RewriteBase /
的RewriteCond $ l ^(index.php文件|资源| robots.txt的)!
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^(。*)$ /index.php?/$1 [L]
 

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

现在我没有得到一个错误页面,从我的主人,但我从codeIgniter得到一个错误页面。

请帮忙。

解决方案

考虑到你的超文本文件的访问应该通过 被路由一切的index.php 反正,最好是用处理您重定向codeIgniter的 重定向() 的功能。

这是例子,下面提供的基础上,我有我的页面的控制器,在我自己的框架扩展(它可用于任何地方在 重定向( ) 的功能是可用的,以及 $这个 - > URI的>部分

 函数handle_redirects(){
    $重定向=阵列(
        pagename.php'=>家/ category_images / 9'
    );
    $ URI =破灭('/',$这个 - > URI的>段);
    的foreach($重定向为$从=> $到)
    {
        从$ = str_replace函数(':任何,str_replace函数('+'':NUM','[0-9] +',从$));
        如果(preg_match(#^ {$从} $#我,$ URI))
        {
            如果(strpos($到,'$')!== false并且strpos($的,'(')!= FALSE)
                $为= preg_replace(#^ {$从} $#我,$到,$ URI);
            重定向($到,位置,301);
        }
    }
}
 

如果你要查询传递到您的 pagename.php 在你的框架文件,你可以考虑以下(请注意,我做不知道你的网页的本意是 - 这是一个通用的解决方案):

  $重定向=阵列(
    '?pagename.php Q =(:任何)'=>家/ category_images / $ 1'
);
 

例如:

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

将您重定向到:

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

该解决方案的工作对我来说相当不错,而且是非常有效的,当涉及到跨浏览器兼容。

请注意,您的规则集应该是这样的,或类似的东西吧:

  RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{} REQUEST_FILENAME -f
重写规则(XML | TXT | CSS | JS | JPG | PNG | GIF)$  -  [L]
重写规则。*的index.php [L,QSA]
 

  

修改:以上已被更改,以适应资产。基本上,它告诉Apache路由一切通过的index.php ,除非它是存在,并有指定的扩展名的文件。 (这是出于安全方面的良好做法。)

     

修改:请注意,您应该不包括的index.php 在您的网址,因为它正在被剥离的.htaccess 文件反正。

请注意,我用PHP 5.3 - 虽然这个解决方案应该为你工作。请让我知道,如果有任何问题。

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.

解决方案

Considering that your Hypertext Access file should be routing everything through index.php anyway, it is best to handle your redirects using CodeIgniter's redirect() function.

An example is provided below, based on what I have in my Page controller, in my own framework extensions (it can be used anywhere where the redirect() function is available, as well as $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);
        }
    }
}

If you were to pass a query to your pagename.php file in your framework, you could consider the following (note that I do not know what the intention of your pages are - this is a generic solution):

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

For example:

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

would redirect you to:

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

This solution works for me quite well, and is very efficient when it comes to cross-browser compatibility.

Note that your RewriteRules should look like this, or something similar to it:

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

Edit: The above has been changed to accommodate for assets. Basically, it tells Apache to route everything through index.php, unless it is a file that exists AND has the extensions specified. (This is good practice for security purposes.)

Edit: Note that you should not include index.php in your URL as it is being stripped by the .htaccess file anyway.

Note that I use PHP 5.3 - though this solution should work for you. Please let me know if there are any problems.

这篇关于301重定向与codeIgniter旧网页的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆