【问题标题】:Rewrite rules for WordPress重写 WordPress 的规则
【发布时间】:2012-03-10 17:44:13
【问题描述】:

我的主题上有一个自定义页面,根据给定的参数显示不同。该页面被设置为网站的主页。 有效参数组合如下:

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

我正在尝试将这些链接转换为以下内容:

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

我的 .htaccess 文件现在看起来像这样,但它似乎不起作用:

<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>

我在这里做错了什么?

谢谢, 拉度

【问题讨论】:

    标签: apache wordpress .htaccess mod-rewrite


    【解决方案1】:

    同样的问题,我使用由“Kyle E Gentile”创建的类丢失了链接。

    但他的课在下面。 创建一个文件: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 文件中,

    //-------------------------------------------------
    //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
    //-------------------------------------------------
    

    可能值得研究.. 我知道它适用于你的前两个变量,fa & type 但是.. 不知道你是否可以重写一点来添加ID,

    马蒂

    【讨论】:

      猜你喜欢
      • 2012-11-21
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多