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

2019-09-06 17:01发布

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

2条回答
乱世女痞
2楼-- · 2019-09-06 17:35
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.

查看更多
来,给爷笑一个
3楼-- · 2019-09-06 17:37

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.

查看更多
登录 后发表回答