Apache URL重写分页

时间:2012-08-29 07:46:04

标签: apache .htaccess rewrite

我不擅长Apache URL重写。我想在这里做一些改写。

http://www.mydomain.com/item/search?words=something&number=1

=>

http://www.mydomain.com/item/search/something/1

也应该避免http://www.mydomain.com/search/something/1 => http://www.mydomain.com/item/search/something/1(如果没有/item/添加它)

这是我的重写代码。

RewriteRule ^search/(.*)/(.*)/?$ /item/search/$1/$2 [L,R=301]
RewriteRule ^(item/search)/([^/]+)/(\d+)/?$ item/search?words=$2&number=$3

显示404 :(

更新:这是我的.htaccess文件:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteRule ^comment/(\d+)/?$ /post/comment/$1 [L,R=301]
RewriteRule ^(post/comment)/(\d+)/?$ post/comment?comment=$2

RewriteRule ^rss/(.*)/?$ /item/rss/$1 [L,R=301]
RewriteRule ^(item/rss)/(.*).xml/?$ item/rss.php?rss=$2

RewriteRule ^search/(.*)/(.*)/?$ /item/search/$1/$2 [L,R=301]
RewriteRule ^(item/search)/([^/]+)/(\d+)/?$ item/search?words=$2&number=$3

RewriteCond %{http_host} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

1 个答案:

答案 0 :(得分:1)

您无法在htaccess中执行此操作,您应该在wordpress主题function.php中添加一些代码。

function add_my_rewrite() {
    add_rewrite_rule('^item/search/(.+)/(\d+)?$', 'index.php?pagename=item/search&words=$matches[1]&number=$matches[2]', 'top');
}
add_action( 'init', 'add_my_rewrite' );

有关WordPress的rewriterules的更多信息:

http://codex.wordpress.org/Rewrite_API

http://codex.wordpress.org/Rewrite_API/add_rewrite_tag

http://codex.wordpress.org/Rewrite_API/add_rewrite_rule