2014-09-30 131 views
1

我试图重写,像这样通过以下网址后(%2F/):WordPress的URL重写 - URL的一部分现有页面

/archives/JHW 
-> /archives?RefNo=JHW 

/archives/JHW/1/1 
-> /archives?RefNo=JHW%2F1%2F1 

/archives 
-> no redirect 

的想法是让网址好看,但随后能够从/archives页面获取$_GET['RefNo']的剩余路径,该页面已存在as a page。到目前为止,我得到了:

RewriteRule ^archives/(.+)/? /index.php/archives/?RefNo=$1 [QSA,L] 

但是,这似乎并没有工作。

我一直是这样的代码将它添加我functions.php里面我的主题:

add_rewrite_rule(
    'archives/(.+)/?', 
    'index.php/archives?RefNo=$1', 
    'top' 
); 

,然后去了Permalinks页面/wp-admin使其再生.htaccess文件。当我看着.htaccess这条规则就在那里时,它就像我所期待的那样没有踢进去。

我哪里错了?


更新

我现在.htaccess文件是:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/
    RewriteRule ^archives/(.+)/? /index.php/archives/?RefNo=$1 [QSA,L] 
    RewriteRule ^index\.php$ - [L] 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule . /index.php [L] 
</IfModule> 
+0

你可以发布你目前的Wordpress'htaccess吗?另外,由于你说它是一个现有的页面,在哪里定义了'/ archives'? – 2014-10-01 09:00:16

+0

它的字面意思是[一个现有的页面](http://grab.by/APRE) - 我已经用当前的htaccess更新了这个问题 – Joe 2014-10-01 09:03:55

+0

好的。但你的规则呢?你正在尝试重写'/index.php/archives/?RefNo = XXX'(通过Wordpress),并且在你提到的问题的开头提到'/ archives?RefNo = XXX'。哪一个是使用的? – 2014-10-01 09:06:06

回答

0

我已经偶然发现一个可能的解决方案是这样的(内部主题的functions.php):

function archives_page_rewrites() { 
    add_rewrite_tag('%RefNo%', '([^&]+)'); 
    add_rewrite_rule(
     '^archives/(.*)/?$', 
     'index.php?pagename=archives&RefNo=$matches[1]', 
     'top' 
    ); 
} 
add_action('init', 'archives_page_rewrites'); 

我愿意相信还有其他方法可以做到这一点,而这可能是也可能不是最好的解决方案。如果在良好实践方面是好的/不好的话,请随意上/下。