301重定向使用CodeIgniter旧网页网站(301 redirect for old page

2019-09-30 04:55发布

我刚才升级我的静态网站启用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:现在从迈克尔先生的建议后,我试图用这样的重定向功能来实现:

默认的控制器功能: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

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

请帮忙。

Answer 1:

考虑到您的超文本文件的访问应通过路由一切index.php无论如何,最好使用CodeIgniter的处理您的重定向redirect()函数。

一个例子如下提供,基于我在我的页面控制器,在我自己的框架扩展(它可以在任何地方使用,其中的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]

编辑 :上面已经改变,以适应资产。 基本上,它告诉阿帕奇路线一切通过index.php ,除非它是存在并具有指定的扩展名的文件。 (这是为了安全起见,好的做法。)

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

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



Answer 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]


Answer 3:

你可以打开你XAMPP /阿帕奇/ conf目录/ httpd.conf文件

然后经过检查以下线

#LoadModule rewrite_module modules/mod_rewrite.so

如果散列(#)的线之上存在英寸 然后将其删除。

LoadModule rewrite_module modules/mod_rewrite.so

并重新启动Apache服务器,并检查你的网站。

之后则无法运作的网站,请让我知道。



文章来源: 301 redirect for old pages of the website with codeIgniter