2013-04-17 24 views
0

客戶端的基於WordPress的網站有員工網頁使用URL這樣的:
/bio/john-doe/
/bio/nim-rod/
等等重寫規則不使用火柴

但在他們的印刷宣傳冊,他們希望名片網址如:
/nim.rod
並讓網站顯示/bio/nim-rod/頁面。

根據客戶這個用於工作。插件文件夾中有一個php文件,它看起來像這個功能的重寫規則(見下文),但我無法讓它工作。

我研究這個問題,並發現this post WordPress的支持網站,但建議的修復不起作用:

試圖解決我加了一些測試規則的代碼問題,比如這條規則確實重定向/any.name/bio/nim-rod/頁面,

$newrules['([a-zA-Z]+)\.([a-zA-Z]+)'] = 'index.php?pagename=nim-rod'; 

所以重寫模塊找到自定義規則和顯示/bio/nim-rod/頁面, 但使用正則表達式匹配現有的規則最終顯示該網站的404錯誤頁面。因此,用正則表達式模式替換匹配的值似乎有些問題,但我找不到任何語法錯誤,我不知道如何進一步排除故障。現有的模式看起來應該起作用,不是嗎?

大約6個月前,服務器出現硬件問題,我們以前的WordPress/PHP專家重建了網站並將其升級到版本3.3.1。我不知道舊版本是什麼,但新版本可能會打破這個功能。

是否有故障診斷提示,您可以指向我?
有什麼工具可以用來調試或檢查正則表達式的工作方式嗎?

<?php 
/** 
* @package Custom_Redirector 
* @version 1.0.0 
*/ 
/* 
Plugin Name: Custom Redirector 
Plugin URI: http://wordpress.org/# 
Description: 
Version: 1.0.0 
Author URI: http://ma.tt/ 
*/ 

add_filter('rewrite_rules_array','wp_insertMyRewriteRules'); 
add_filter('query_vars','wp_insertMyRewriteQueryVars'); 
add_filter('init','flushRules'); 

// Remember to flush_rules() when adding rules 
function flushRules(){ 
    global $wp_rewrite; 
    $wp_rewrite->flush_rules(); 
} 

// Adding a new rule 
function wp_insertMyRewriteRules($rules) 
{ 
    $newrules = array(); 
    $newrules['([a-zA-Z]+)\.([a-zA-Z]+)'] = 'index.php?pagename=$matches[1]-$matches[2]'; 
    return $newrules + $rules; 
} 

// Adding the id var so that WP recognizes it 
function wp_insertMyRewriteQueryVars($vars) 
{ 
    array_push($vars, 'id'); 
    return $vars; 
} 
?> 

回答

0

試試這個;

([a-zA-Z]+)(?:\.|-)([a-zA-Z]+) 

順便說一句,該字符串匹配這兩個東西index.php?pagename=nim-rod

index.php頁面名稱= nim-rod

如果你不希望出現這種情況,那麼使用;

([a-zA-Z]+)-([a-zA-Z]+) 
+0

嗨Allendar,感謝您的建議,但它並沒有幫助。我不認爲這個問題是與URL正則表達式規則,但與重寫表達式使用$匹配數組。這不是創建預期的網址,而是系統顯示它的404錯誤頁面。 – Dan