重写和分页交互

时间:2014-12-22 10:21:00

标签: php .htaccess mod-rewrite pagination

我正在使用previous question中的重写角色在index.phpsingle.php之间切换。现在我希望在每个类别的index.php中加入分页。 例如,如果我想要显示

www.example.com/cat1/page=2 => www.example.com/cat1/index.php?page=2

这将转到

www.example.com/cat1/page=2  =>www.example.com/cat1/single.php?page=2 

我如何解决这种互动?
从这个问题我有这个角色:


mydomain-|
         |
         |
         |__cat1-|__.htaccess  // cat1 htaccess
         |       |__index.php
         |       |__single.php
         |
         |__cat2-|__.htaccess // cat2 htaccess
                 |       |__index.php
                 |       |__single.php
                ...
                 |__.htaccess  // root htaccess
                 |__index.php  // home-page 

DocumentRoot/.htaccess:

<IfModule mod_rewrite.c>

<files ".htaccess">
order allow,deny
deny from all
</files>    
Options All -Indexes

RewriteEngine On
RewriteBase /

# Redirect from www.
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,NE,R=301]

# 404 for index.php in URL
RewriteCond %{THE_REQUEST} /index\.php[\s?/] [NC]
RewriteRule ^ - [L,R=404]

</IfModule>

/cat1/.htaccess:

Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /cat1/

RewriteCond %{THE_REQUEST} /index\.php[?\s] [NC]
RewriteRule ^index\.php$ - [L,R=404]

RewriteRule ^(\d+)/([^/]+)/?$ single.php?id=$1&title=$2 [L,QSA]

</IfModule>

1 个答案:

答案 0 :(得分:1)

将您的/cat1/.htaccess更改为:

Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /cat1/

RewriteCond %{THE_REQUEST} /index\.php[?\s] [NC]
RewriteRule ^index\.php$ - [L,R=404]

RewriteRule ^(page=\d+)/?$ index.php?$1 [L,NC,QSA]

RewriteRule ^(\d+)/([^/]+)/?$ single.php?id=$1&title=$2 [L,QSA]

</IfModule>