Htaccess rule to make urls like this ?page=1 look like /page/1 in Codeigniter

六月ゝ 毕业季﹏ 提交于 2019-12-12 03:08:31

问题


This is how my urls currently look:

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

How can I make this work?:

http://mysite.com/page/1

There is a post on StackOverflow that asks the same question. But the accepted solution isn't working for me. Because I am using Codeigniter and my page results in a 404 perhaps because since the url pattern of a CI site is:

domain/controller/method

The system is assuming that I am requesting a controller called "page" and a method called "1" both of which of course doesn't exist. Or maybye it's due to a conflict with the other code in my htaccess file (which I downloaded from the CI wiki, it gets rid of index.php and does a few security things). Here is my entire htaccess file:

<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>

The non indented bit is the solution I got from that other SO question that isn't working for me.

Any solutions to this CI pagination issue?


UPDATE

Ok, read some of the docs and now I have this working:

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

What would be the htaccess rule to turn that into?:

http://mysite.com/page/2

回答1:


You should make this configuration at /application/config/routes.php (and let the .htaccess just for hide the index.php as you are already doing).

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

Or better, like @zaherg remembered (ensures that only numbers could by matched):

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

This way all the requests to http://mysite.com/page/2 will be treated internally as http://mysite.com/home/index/2 and so forth.

I suggest you take a look at CodeIgniter User Guide - URI Routing and CodeIgniter User Guide - Tutorial − Introduction.

Good luck.




回答2:


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

from CodeIgniter docs

That will handle removing the index.php, but what happens after that depends how CodeIgniter's query string handling is set up: it can be configured to use a query string rather than a path. See the link for more details.



来源:https://stackoverflow.com/questions/10134050/htaccess-rule-to-make-urls-like-this-page-1-look-like-page-1-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!