简体   繁体   English

使用.htaccess文件删除URL的特定段

[英]Using .htaccess file to remove a particular segment of the URL

My url link looked like: 我的网址链接看起来像:

http://localhost/CodeIgniter_2.1.2/index.php/pages/home

Then I wrote the .htaccess file using google as follows: 然后,我使用Google编写了.htaccess文件,如下所示:

RewriteEngine on
RewriteCond $1 !^(index\.php|themes|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

Now my link looks like: 现在我的链接看起来像:

http://localhost/CodeIgniter_2.1.2/pages/home

Now,I want to remove pages from this link. 现在,我想从此链接中删除pages Can anyone help me figure this out? 谁能帮我解决这个问题?

So, my url could look like: 因此,我的网址可能类似于:

http://localhost/CodeIgniter_2.1.2/home

One way to do it is to create a custom route in the application/config/routes.php file, which routes the url 'http://localhost/CodeIgniter_2.1.2/home' to 'http://localhost/CodeIgniter_2.1.2/pages/home' 一种实现方法是在application / config / routes.php文件中创建一个自定义路由,该路由会将url'http://localhost/CodeIgniter_2.1.2/home'路由到'http://localhost/CodeIgniter_2.1.2 / pages / home'

Custom routing is described here 自定义路由在此处介绍

The code will look something like: 该代码将类似于:

$route['home'] = "pages/home";

EDIT 编辑

Your controller, method, and variable are named "pages", "view", and "home" respectively. 您的控制器,方法和变量分别命名为“页面”,“视图”和“主页”。 Therefore, you should try the following route instead: 因此,您应该尝试以下路线:

$route['home'] = "pages/view/home";

Also: 也:

** Reserved Routes ** From the codeigniter documentation: **保留的路线**从codeigniter文档中:

There are two reserved routes: 有两条保留的路线:

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

This route indicates which controller class should be loaded if the URI contains no data, which will be the case when people load your root URL. 此路由指示如果URI不包含任何数据,则应该加载哪个控制器类,当人们加载您的根URL时就是这种情况。 In the above example, the "welcome" class would be loaded. 在上面的示例中,将加载“ welcome”类。 You are encouraged to always have a default route otherwise a 404 page will appear by default. 建议您始终使用默认路由,否则默认情况下会显示404页面。

Thus, you should not be setting the default controller to "pages/view/home." 因此,您不应将默认控制器设置为“ pages / view / home”。 Rather, you should create an "index" method in the controller that defaults to the "home" view. 相反,您应该在控制器中创建一个默认为“主”视图的“索引”方法。

Also Don't forget to change the $config['index_page'] from 'index.php' to '' in the config.php file located in application/config/config.php. 同样不要忘记将位于application / config / config.php中的config.php文件中的$ config ['index_page']从'index.php'更改为''。

尝试更改您的规则,以便目标是:

RewriteRule ^(.*)$ index.php/pages/$1 [L]

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

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