0

我不擅长 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
4

1 回答 1

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 重写规则的更多信息:

http://codex.wordpress.org/Rewrite_API

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

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

于 2012-08-29T15:17:46.953 回答