-1

i just created a new wordpress page template that runs some php&mysql scripts inside it and i would like to apply mod rewrite to it's subpages.

for instance i have the following link generated:

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

how can i transform this into something like:

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

in my htaccess i have the default code generated by wordpress ...

does anyone know how to achieve that?

Steff
  • 1
  • 1

3 Answers3

1

Try this:

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]
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
0

You'll have to add the following to your .htaccess file:

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

This won't prettify URLs that don't have all three variables, but that's easy enough to add to this one or simply put in an additional rule if needed.

Dan Ambrisco
  • 865
  • 7
  • 13
  • RewriteRule cannot deal with query string parameters – Tony McCreath Feb 16 '12 at 12:45
  • 1
    Obviously it can't handle them on the incoming string, but we're rewriting TO the query string which it can do. In fact, if it couldn't do that it would be nearly impossible to do much anything much useful with mod_rewrite. – Dan Ambrisco Feb 17 '12 at 08:52
  • @Tiggerito - Unless you're confused by the `?!` at the start of the first group, which is simply a negated group meaning that if that group is matched then the match as a whole is voided. – Dan Ambrisco Feb 20 '12 at 09:40
  • I'd got the requirement backwards! – Tony McCreath Feb 27 '12 at 04:45
0

I was using this little class help on a recent project.. have to say it worked brilliant.. was written by: 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;
        }

    }

}
?>

To use this class you must first include the file.
After including the file, you need to create an options array.

<?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') );
?>

this will then allow you to use the url http://www.quotist.com/quotes-by-authors/letter/d/

Marty

Marty
  • 4,619
  • 2
  • 24
  • 35