Htaccess规则制作这样的网址?page = 1在Codeigniter中看起来像/ page / 1

时间:2012-04-13 01:13:37

标签: .htaccess codeigniter url mod-rewrite friendly-url

这就是我的网址目前的样子:

http://mysite.com/?page=1

我该如何做到这一点?:

http://mysite.com/page/1

StackOverflow上有一个post会询问相同的问题。但是接受的解决方案并不适用于我。因为我使用Codeigniter而我的页面结果是404,因为CI网站的url模式是:

域/控制器/方法

系统假设我正在请求名为“page”的控制器和一个名为“1”的方法,这两种方法当然都不存在。或者可能是因为我的htaccess文件中的其他代码发生了冲突(我从CI wiki下载了它,它摆脱了index.php并做了一些安全事情)。这是我的整个htaccess文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users. Additionally this will allow you to create a System.php controller, 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder. This snippet prevents user access to the application folder. Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file, such as an image or css document, if this isn't true it sends the request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]

#Pretty urls for pagination links
RewriteRule (.*) index.php?page=$1

</IfModule>

非缩进位是我从那个不适合我的其他SO问题中得到的解决方案。

此CI分页问题的任何解决方案?


更新

好的,阅读一些文档,现在我有了这个工作:

http://mysite.com/home/index/2

什么是htaccess规则将其变成?:

http://mysite.com/page/2

2 个答案:

答案 0 :(得分:1)

您应该在/application/config/routes.php进行此配置(让.htaccess只是为了隐藏index.php)。

$route['page/(:any)'] = 'home/index/$1';

或者更好,就像@zaherg记得的那样(确保只匹配数字):

$route['page/(:num)'] = 'home/index/$1';

这样,http://mysite.com/page/2的所有请求都将在内部处理为http://mysite.com/home/index/2,依此类推。

我建议您查看CodeIgniter User Guide - URI RoutingCodeIgniter User Guide - Tutorial − Introduction

祝你好运。

答案 1 :(得分:0)

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

来自CodeIgniter docs

这将处理删除index.php,但之后会发生什么取决于CodeIgniter的查询字符串处理是如何设置的:它可以配置为使用查询字符串而不是路径。有关更多详细信息,请参阅链接。