简体   繁体   English

在codeigniter中将URL重定向到另一个

[英]Redirect the URL to another, in codeigniter

I have a webpage which was built with core php, now its been updated to codeigniter mvc. 我有一个使用核心php构建的网页,现在将其更新为codeigniter mvc。 Problem which i am facing is, users are trying to enter the old URL path 我面临的问题是,用户正在尝试输入旧的URL路径

example: www.website_name/old_path 例如:www.website_name / old_path

now since the mvc layout, it wont work and will get 404 error. 现在,由于mvc布局,它将无法正常工作,并且会出现404错误。 i want to redirect the clients who enters www.website_name/old_path for the new one 我想重定向输入新的www.website_name / old_path的客户端

www.website_name/index.php/new_controller/new_view. www.website_name / index.php / new_controller / new_view。

Anyone please help me to resolve this issue. 任何人都可以帮助我解决此问题。

Thank You! 谢谢!

first of all remove index.php config file. 首先删除index.php配置文件。 Open config.php and do following replaces 打开config.php并执行以下替换

$config['index_page'] = "index.php"

to

$config['index_page'] = ""

Then create HTACCESS file in application/ folder 然后在application /文件夹中创建HTACCESS文件

having this code.... 有此代码...。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>

Now index.php is removed from your URl.... 现在index.php已从您的URl中删除。...

STEP 2) open application/config/routes.php file Now here define your desired web URL 步骤2)打开application / config / routes.php文件现在在此处定义所需的Web URL

$route['mycontroller/(:any)'] = "mycontroller/user_detail/$1";

So,Your URL Will be .. www.website_name/new_controller(or xyz)/ 因此,您的URL将是.. www.website_name / new_controller(或xyz)/

Here you can also change the controller name differently using routes So no one can get the exact name of controller/method name....To Do such customization refer this URL http://www.codeigniter.com/user_guide/general/urls.html 在这里,您还可以使用路由以不同的方式更改控制器名称,因此没人能获得控制器/方法名称的确切名称。...要进行此类自定义,请参考以下URL http://www.codeigniter.com/user_guide/general/urls .html

You can use 301 redirect for this type of issue. 您可以对这种类型的问题使用301重定向。

Put this code in .htaccess 将此代码放在.htaccess中

RewriteRule ^www.website_name/old_path www.website_name/index.php/new_controller/new_view [L,R=301]

or use 301 redirect of codigniter http://www.codeigniter.com/user_guide/helpers/url_helper.html 或使用codigniter的301重定向http://www.codeigniter.com/user_guide/helpers/url_helper.html

// with 301 redirect
redirect('/article/13', 'location', 301);

As Sumit mentioned, you should use URL Routing for this. 如Sumit所述,您应该为此使用URL路由。

In the routes file you can create a list of all the redirects that you want like so: 在路由文件中,您可以像这样创建所有重定向的列表:

$route['old_path'] = "new_controller/new_view";

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

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