简体   繁体   English

将错误的URL重定向到指定的URL代码初始化器

[英]Redirecting wrong URL to specif URL codeigniter

This is my current folder structure: 这是我当前的文件夹结构:

Description: 描述:

  • My base_url is: localhost/shoppingCart 我的base_url是: localhost/shoppingCart
  • My default controller is Welcome 我的默认控制器是Welcome

I have divided my project into 2 parts: front end and back end. 我将项目分为两个部分:前端和后端。

For back end my standard controller is "admin/home" (admin is folder and home is the name of controller) 对于后端,我的标准控制器是“ admin / home”(admin是文件夹,home是控制器的名称)

For front end my standard controller is "welcome" (welcome is name of controller) 对于前端,我的标准控制器为“欢迎”(欢迎使用控制器的名称)

Problem: 问题:

When I enter localhost/ShoppingCart/admin it is going to "welcome" controller but the url remains same. 当我输入localhost/ShoppingCart/admin它将转到“欢迎”控制器,但URL保持不变。 I should throw some error I think. 我认为应该抛出一些错误。

And when I enter localhost/ShoppingCart/admin/home it is working properly. 当我输入localhost/ShoppingCart/admin/home它可以正常工作。

I tried using route to redirect localhost/ShoppingCart/admin to localhost/ShoppingCart : 我尝试使用路由将localhost/ShoppingCart/admin重定向到localhost/ShoppingCart

$route['/admin'] = "welcome";

To redirect a subfolder to your main domain, you'll have to do something like this : 要将子文件夹重定向到您的主域,您必须执行以下操作:

#Redirect a sub folder to root
Redirect 301 /subfolder /

In your case it will be : 您的情况将是:

redirect 301 /admin /

I guess that your problem is on the routes. 我想您的问题出在路线上。

Also I recommend you to use a "Good" names from controllers Naming convention Wikipedia 我也建议您使用控制器的“好”名称命名约定Wikipedia

"welcome" and "home" are not two different things as back-end and Front-end are. “欢迎”和“家庭”与后端和前端不是两个不同的事物。

now about the code 现在关于代码

I assume that your routes.php file contains something like 我假设您的routes.php文件包含类似

$route['default_controller'] = "welcome";

now, for each sub-folder in your controller's folder you must set a default controller in routes.php the $route['default_controller'] is not working in sub folders. 现在,对于控制器文件夹中的每个子文件夹,必须在routes.php设置一个默认控制器, $route['default_controller']在子文件夹中不起作用。

for you, will be something like 对你来说,就像

$route['admin'] = 'admin/home'

note, remember to set only the name that you want to use, about the symbols / the framework is going to manage them. 请注意,请记住仅设置要使用的名称,关于符号/框架将要管理它们的名称。

which in the url will be http://localhost/ShoppingCart/admin 网址中的哪个是http:// localhost / ShoppingCart / admin

Every subdir in APPPATH.'controllers' dir should have controller that is named as same as default controller in APPPATH.'route.php' . APPPATH.'controllers'目录中的每个APPPATH.'controllers'都应具有与APPPATH.'route.php'默认控制器相同名称的控制器。 If you would rename admin module Home.php to Welcome.php (class inside too), you would solve this issue. 如果您将管理模块Home.php重命名为Welcome.php (也包含内部类),则可以解决此问题。 This is because, when router doesn't find Admin.php file for first uri segment, it seeks for admin directory. 这是因为,当路由器找不到第一个uri段的Admin.php文件时,它将寻找admin目录。 When finds it, if there is no next segment in url, router will seek for default controller method set in config.php file. 当找到它时,如果URL中没有下一个段,则路由器将寻求在config.php文件中设置的默认控制器方法。 It's easy to solve having unified name for home or welcome page in every subdir as well as in controllers dir. 解决每个子目录以及controllers目录中的主页或欢迎页面统一名称很容易。

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

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