重写的Word preSS规则 [英] Rewrite rules for WordPress

查看:288
本文介绍了重写的Word preSS规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示不同根据给定的参数对我的主题自定义页面。此页面设置为主页的网站。 有效参数的组合可以是如下:

I have a custom page on my theme that displays differently based on given parameters. This page is set as homepage for the website. Valid parameters combinations can be as follows:

my_domain/?fa={action}&type={type}
my_domain/?fa={action}&id={type}
my_domain/?fa={action}

我想要做的是将这些链接转换为以下内容:

What i'm trying to do is to convert these links to the following:

my_domain/fa/{action}/type/{type}
my_domain//fa/{action}/id/{type}
my_domain/fa/{action}

我的.htaccess文件现在看起来是这样,但它似乎并没有工作:

My .htaccess file now looks like this, but it doesn't seem to work:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^fa/(.*)/type/(.*)$ /index.php [L]
RewriteRule ^fa/(.*)/id/(.*)$ /index.php?fa=$1&fid=$2 [L]
RewriteRule ^fa/(.*)$ /index.php?fa=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

我究竟做错了什么?

What am i doing wrong here?

谢谢, 拉杜

推荐答案

同样的问题,我用'凯尔Ë外邦人创建的类失去了联系。

same issue, I used a class created by 'Kyle E Gentile' lost the link.

但他的类如下。 创建一个文件:add_rewrite_rules.php

but his class is below. create a file: add_rewrite_rules.php

//prevent duplicate loading of the class if you are using this in multiply plugins
if(!class_exists('add_rewrite_rules')){

    class Add_rewrite_rules{

        var $query_vars;
        var $rules;

        function __construct($options){
            $this->init($options);
        }

        function init($options){
            foreach($options as $key => $value){
                $this->$key = $value;
            }
        }

        function rules_exist(){
            global $wp_rewrite;

            $has_rules = TRUE;

            foreach($this->rules as $key => $value){
                if(!in_array($value, $wp_rewrite->rules)){
                    $has_rules = FALSE;
                }   
            }

            return $has_rules;
        }

        //to be used add_action with the hook 'wp_head'
        //flushing rewrite rules is labor intense so we better test to see if our rules exist first
        //if the rules don't exist flush its like after a night of drinking  
        function flush_rules(){
            global $wp_rewrite;

            if(!$this->rules_exist()){
                //echo "flushed"; // If want to see this in action uncomment this line and remove this text and you will see it flushed before your eyes
                $wp_rewrite->flush_rules();
            }
        }

        //filter function to be used with add_filter() with the hook "query_vars"
        function add_query_vars($query_vars){

            foreach($this->query_vars as $var){
                $query_vars[] = $var;
            }

            return $query_vars;
        }

        //to be used with a the add_action() with the hook "generate_rewrite_rules"
        function add_rewrite_rules(){
            global $wp_rewrite;

            $wp_rewrite->rules = $this->rules + $wp_rewrite->rules;
        }

    }

}

然后在funcitons.php文件,

then in your funcitons.php file,

//-------------------------------------------------
//ADDING REWRITE RULES AND QUERY VARS
//-------------------------------------------------
include('add_rewrite_rules.php');
$options = array(
        'query_vars' => array('fa', 'type'),
        'rules' => 
            array(
                '(.+?)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=$matches[1]&fa=$matches[2]&type=$matches[3]'
            )
    );
$rewrite = new Add_rewrite_rules($options);
add_action('wp_head', array(&$rewrite, 'flush_rules'));
add_action( 'generate_rewrite_rules', array(&$rewrite, 'add_rewrite_rules') );
add_filter( 'query_vars', array(&$rewrite, 'add_query_vars') );
//-------------------------------------------------
//ADDING REWRITE RULES AND QUERY VARS
//-------------------------------------------------

可能是值得探讨.. 我知道它会为你的前两个变量,足总杯和放大器;类型,但.. 不知道你是否可以重新写一点,添加ID,

could be worth looking into.. i know it will work for your first two variables, fa & type but.. not sure if you could re-write a little to add the ID,

马蒂

这篇关于重写的Word preSS规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆