2016-12-29 21 views
1

我有一個嚴重的問題與我的WP網站之一。 Plaese看看我的重寫規則:WP:如何add_rewrite_rule()不創建額外的頁面

function set_rewrite_rules(){ 
    add_rewrite_tag('%posttype%', '([^&]+)'); 
    add_rewrite_rule('^expertise[/]([^/]+)[/]([^/]+)[/]page[/]([0-9]+)[/]?', 
    'index.php?expertise=$matches[1]&posttype=$matches[2]&paged=$matches[3]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]*)[/]articles[/]([^/]+)[/]?', 
    'index.php?article=$matches[2]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]*)[/]verdicts[/]([^/]+)[/]?', 
    'index.php?verdict=$matches[2]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]*)[/]laws[/]([^/]+)[/]?', 
    'index.php?law=$matches[2]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]*)[/]prosecutions[/]([^/]+)[/]?', 
    'index.php?prosecution=$matches[2]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]+)[/]([^/]+)[/]?', 
    'index.php?expertise=$matches[1]&posttype=$matches[2]', 
    'top'); 
} 
add_action('init', 'set_rewrite_rules'); 

它的偉大工程,但外幣很多額外的頁面,如:

/expertise/[EX-NAME]/page/2/page/4/page/3/page/2/page/4/page/2/ 

通常應返回404,但現在這些網頁作品&看起來像一個正常的有效頁面。 預期結果:分頁只能與一次匹配一起使用。例如:

/expertise/[EX-NAME]/page/2/ 

一切正常,而set_rewrite_rules被註釋掉。

+1

嘗試在第一個重寫規則結束時添加一個'$',看看會發生什麼。 – revo

+0

@revo試過了。 [/]([^ /] +)[/]([^ /] +)[/]頁面[/]([0-9] +)[/] ?'','index.php?expertise = $ matches [1]&posttype = $ matches [2]&paged = $ matches [3]','top');' –

+0

如果您刪除了這個特定的重寫規則? – revo

回答

0

試圖將$添加到最後一個規則&它看起來沒有額外的頁面。

見例如:

function set_rewrite_rules(){ 
    add_rewrite_tag('%posttype%', '([^&]+)'); 
    add_rewrite_rule('^expertise[/]([^/]+)[/]([^/]+)[/]page[/]([0-9]+)[/]?', 
    'index.php?expertise=$matches[1]&posttype=$matches[2]&paged=$matches[3]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]*)[/]articles[/]([^/]+)[/]?', 
    'index.php?article=$matches[2]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]*)[/]verdicts[/]([^/]+)[/]?', 
    'index.php?verdict=$matches[2]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]*)[/]laws[/]([^/]+)[/]?', 
    'index.php?law=$matches[2]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]*)[/]prosecutions[/]([^/]+)[/]?', 
    'index.php?prosecution=$matches[2]', 
    'top'); 
    add_rewrite_rule('^expertise[/]([^/]+)[/]([^/]+)[/]?$', 
    'index.php?expertise=$matches[1]&posttype=$matches[2]', 
    'top'); 
} 
add_action('init', 'set_rewrite_rules'); 

感謝@revo的幫助!

+0

如果評論有助於答案。要求該用戶創建一個答案,以便您可以向正確的用戶獎勵點數。現在你真的不能 – janw