-1

我刚刚创建了一个新的 wordpress 页面模板,它在其中运行一些 php&mysql 脚本,我想将 mod rewrite 应用到它的子页面。

例如,我生成了以下链接:

http://www.quotist.com/quotes-by-authors.html?letter=D

我怎样才能把它变成这样的东西:

http://www.quotist.com/quotes-by-authors/letter/d/

在我的 htaccess 中,我有 wordpress 生成的默认代码...

有谁知道如何做到这一点?

4

3 回答 3

1

尝试这个:

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteCond %{QUERY_STRING} ^(letter)=(\w)$ [NC]
RewriteRule ([^.]+)\.html http://www.quotist.com/$1/%1/%2? [L,R=301]

RewriteCond %{REQUEST_URI} ^/([\w-]+)/(letter)/\w/? [NC]
RewriteRule ^ /%1.html?%2=%3 [L,QSA]
于 2012-02-16T11:09:14.350 回答
0

您必须将以下内容添加到您的 .htaccess 文件中:

RewriteRule ^(?![^.]+\.html.*)([^/]+)/([^/]+)/([^/]+)/?$ $1.html?$2=$3 [L,QSA]

这不会美化没有所有三个变量的 URL,但这很容易添加到这个变量中,或者在需要时简单地添加一个附加规则。

于 2012-02-16T10:40:44.237 回答
0

我在最近的一个项目中使用这个小类帮助..不得不说它工作得很好..作者:Kyle E Gentile

<?php
// FILENAME: add_rewrite_rules.php
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;
        }

    }

}
?>

要使用此类,您必须首先包含该文件。
包含文件后,您需要创建一个选项数组。

<?php
include(YOURPLUGIN_ABSPATH.'/add_rewrite_rules.php');
$options = array(
        'query_vars' => array('letter'),
        'rules' => 
            array(
                '(.+?)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=$matches[1]&letter=$matches[2]'
            )
    );
$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') );
?>

这将允许您使用网址 http://www.quotist.com/quotes-by-authors/letter/d/

马蒂

于 2012-02-16T11:32:08.823 回答