WordPress url rewrite hack plugin

There is a simple way to push your rewrite rules to wordpress using a simple filter. To do this you need use rewrite_rules_array filter. Before starting with that let me explain the basic rewrite structure of wordpress 2.0 and above.

# BEGIN WordPress
< IfModule mod_rewrite.c >
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
< /IfModule >
# END WordPress

The rules above are the default rules in wordpress when you enable permalink option. The rules checks if the incoming request URI is a physical files or a physical directory. If both the conditions are false it directs the control to index.php. So that ends there. If you type any random url on permalink enabled wordpress blog you will get a 404 error page. This is because wordpress tries to decode the incoming url with series of rules and detects if the request is of some pattern. And if there is a pattern match it loads the needed stuff else it load a 404 page.

Basically the default pattern is generated based on what you set on permalink option page. And it has standard pattern rules for loading page, rss, author, category etc etc… The next thing you need to do it is to insert your pattern into wordpress rules array. This is achieved with the help of rewrite_rules_array filter. I will show how you can load your rewrite rules into wordpress.

function insertMyRewriteRules($rules){
$newrules = array();
$newrules[‘redirect/url/(.+)$’]=’index.php?is_page_redirect=1&redirect_url=$matches[1]’;
return $newrules+$rules;
}
add_filter(‘rewrite_rules_array’,’insertMyRewriteRules’);

Using the above function I have added a pattern /redirect/url/(anything) to rewrite rules. If request URI has this pattern then it will pass two variables namely is_page_redirect and redirect_url to index.php. Variable redirect_url will hold the pattern match of string followed after /redirect/url/ in the request uri.
Example if I requested a url imthi.com/redirect/url/http://www.example.com/ redirect_url will hold ‘http://www.example.com’. Now pattern is matched and parsed into index.php as query variables. Now the next is to tell url query parser to use these variables. To do so you have apply a filter query_vars.

function insertMyRewriteQueryVars($vars){
array_push($vars, ‘is_page_redirect’, ‘redirect_url’);
return $vars;
}
add_filter(‘query_vars‘,’insertMyRewriteQueryVars’);

The above function will add the two variables into the query vars and by which you instruct wordpress not to loose if these variables found in the request query. Okay that is done and the variables are retained. Next step is tell the query parser to use this. That is done with the help of action parse_query.

function insertMyRewriteParseQuery($query){
if(!empty($query->query_vars[‘is_page_redirect’])){
header(“location:”.$query->query_vars[‘redirect_url’]);
exit();
}
}
add_action(‘parse_query’,’insertMyRewriteParseQuery’);

The above fuction will check if there is variable is named is_page_redirect in query vars. If present it redirects to location present in variable redirect_url. :-D.. Lets wrap this up and make it a plugin which will apply redirect to comment author website urls.

Download comment url redirect plugin. Dont be surprised it is really simple.. 😉

Previous Article

Free Rss Reader for i-mate

Next Article

Etisalat blocks firefox once again.

View Comments (12)

Comments are closed.