繁体   English   中英

Codeigniter:清理我的URL(删除index.php)之后,我无法将访问我的网站的用户重定向到新的干净URL

[英]Codeigniter: After cleaning my URLs(removing index.php) I couldn't redirect users who accessed my website to the new clean URL

我在努力(由于托管公司,哥达迪)删除了index.php? 完成之后,从我的网站的URL中获取。
之后,我注意到一些用户正在使用旧的URL(带有index.php?/ URL)访问我的网站index.php?/
因此,我试图重定向来自index.php?的用户index.php? 使用301重定向的网页,但没有运气。
我在互联网上搜索了一下
但我认为问题背后的原因是我需要它从所有
包括index.php?/example.com或同一页面请求,但没有index.php?/ (首选)。

例如:请求example.com/index.php?/site/category/etc将导致example.comexample.com/site/category/etc (首选方法)。
并且任何其他URL将被视为相同。

这是我当前的.htaccess文件(与此问题无关):

RewriteEngine On
RewriteBase /

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

更新
尝试php解决方案:(提供给Robin Castlin)
我尝试使用挂钩(基于答案),但没有用! 我从配置启用了hooks ,并且URL助手被自动加载。
这个函数在application/hooks/MY_hooks.php

function redirectToNewURL(){

    if(preg_match('~/index.php?/~', $_SERVER['REQUEST_URI'])) {//check if the URL has index.php 
        header('Location: '.current_url(), true, 301); //redirect
        exit;
    }
}  

这是我设置钩子的方法:
application/config/hooks.php
我把这个简单的数组:

$hook['pre_controller'] = array(
    'class' => '',
    'function' => 'redirectToNewURL',
    'filename' => 'MY_hooks.php',
    'filepath' => 'hooks'
);

最终,我没有结果。

$CI =& get_instance();

$CI->load->helper('url');

if (preg_match("~/index\.php\?/~", $_SERVER['REQUEST_URI'])) {
    header('Location: '.current_url());
    exit;
}

将此添加到一个钩子或自动加载的帮助程序中应做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM