2012-08-29 124 views
0

我不擅長Apache URL重寫。我想在這裏做一些重寫。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 :(

UPDATE:這是我所有的.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 
+0

什麼是您在返回一個404瀏覽器的地址欄中鍵入的網址是什麼? –

+0

@Jon Lin,我輸入:'http:// www.mydomain.com/item/search/something/1'返回404. – cj333

+0

這段代碼位於文檔根目錄下的htaccess文件中嗎?當你進入'http://www.mydomain.com/item/search?words = something&number = 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'); 
有關的RewriteRules

更多信息爲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

+0

太棒了,這個工程。 – cj333