WordPress: Custom URL rewrite and reading URL values
Hello!
Recently I was working for a Irish guy who has opened a wordpress blog few months ago. I did the theme for him. Recently he requested me for modifying and/or upgrading the project. One of the project requirements was URL rewriting. For example, there can a URL as follows:
http://www.sitename.com/index.php?group_name=My-Input-Value-Goes-Here
But after rewrite, the URL should look like:
http://www.sitename.com/My-Input-Value-Goes-Here
Well. You see I am passing the variable value in index.php file. But I needed to read the custom URL passed value through another PHP file and that is group.php. My group.php file was inside the theme folder. I am passing the group_name value in index.php file but reading from group.php file. It sounds a bit interesting. Huh?
OK. After browsing several blogs, I came up to writing a custom plugin. Thanks to Google.com and all those people sharing information.
My plugin made me a way so that I could pass a variable value in index.php file but read it from group.php file. This plugin code is available almost everywhere. Here goes my plugin:
<?php /* Plugin Name: WP Custom URL Plugin URI: http://www.tanzilo.com/# Description: A plugin to allow parameters to be passed in the URL and recognized by WordPress Author: Tanzilo Version: 1.0 Author URI: http://www.tanzilo.com/ */ function flush_rewrite_rules() { global $wp_rewrite; $wp_rewrite->flush_rules(); } function add_rewrite_rules( $wp_rewrite ) { $new_rules = array( '(.+)' => 'index.php?group_name=' . $wp_rewrite->preg_index(1) ); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } function add_query_vars( $qvars ) { $qvars[] = 'group_name'; return $qvars; } function template_redirect_file() { global $wp_query; if ( $wp_query->get('group_name') ) { if (file_exists( TEMPLATEPATH . '/group.php' )) { include( TEMPLATEPATH . '/group.php' ); exit; } } } add_action('init', 'flush_rewrite_rules'); add_action('generate_rewrite_rules', 'add_rewrite_rules'); add_filter('query_vars', 'add_query_vars'); add_action('template_redirect', 'template_redirect_file'); ?>
So, what happens now when I activate this plugin? It does a simple work and that is: when it gets a link as below:
http://www.sitename.com/index.php?group_name=My-Input-Value-Goes-Here
It sends the group_name variable value to group.php file which is in the theme folder. In group.php file, I can catch the variable value and use it anyway I want.
Do you want to see my group.php file too? OK. Here it goes:
<?php get_header(); ?> <?php $group_name = $_GET['group_name']; $group_name = str_replace('-', ' ', $group_name); ?> <div id="container"> <div id="contentLeft"> <div id="pageContent"> <div id="postTitle"><?php echo $group_name; ?></div> <div id="postContent"><?php the_time('F j, Y') ?><br /> <ul id="pageGroupLinks"> <?php echo getGroupPageLinks($group_name); ?> </ul> </div> <div id="rssSubscribe">If you enjoyed this post, make sure you <a href="<?php bloginfo('rss2_url'); ?>">subscribe to our RSS feed!</a> </div> </div> </div> <?php get_sidebar(); ?> </div> <?php get_footer(); ?>
This was half the way winning the battle.
But why? Because the following lines should solve my problem.
function add_rewrite_rules( $wp_rewrite ) { $new_rules = array( '(.+)' => 'index.php?group_name=' . $wp_rewrite->preg_index(1) ); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; }
But for any unknown reason it was not working.
So, I had to take help of apache (dot)htaccess file for solving the rest of the part.
I was searching for a helpful blog link and after searching many blogs, I found a similar blog article and here it is:
http://www.webmasterworld.com/forum92/6079.htm
In this blog, I found a similar solution and wrote my own based on this code.
# Enable mod_rewrite, start rewrite engine Options +FollowSymLinks RewriteEngine on # # Internally rewrite search engine friendly static URL to dynamic filepath and query RewriteRule ^product/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?product=$1&color=$2&size=$3&texture=$4&maker=$5 [L] # # Externally redirect client requests for old dynamic URLs to equivalent new static URLs RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?product=([^&]+)&color=([^&]+)&size=([^&]+)&texture=([^&]+)&maker=([^\ ]+)\ HTTP/ RewriteRule ^index\.php$ http://example.com/product/%1/%2/%3/%4/%5? [R=301,L]
Finally my (dot)htaccess file looked like this:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^pages/([^/]+)/?$ /index.php?group_name=$1 [L] RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?group_name=([^&]+)\ HTTP/ RewriteRule ^index\.php$ http://mobile.ie/pages/%1? [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
But I was not 100% perfect but close to the solution. The reason is my URL started looking like this:
http://www.sitename.com/pages/My-Input-Value-Goes-Here
Well. You see I was almost close to the solution. As the buyer wanted like this:
http://www.sitename.com/My-Input-Value-Goes-Here
But I came up with a solution to:
http://www.sitename.com/pages/My-Input-Value-Goes-Here
I believe you can work on it and come to a solution without the extra “pages/” part in the URL. If you find such a solution, please share with me and others who will face similar problem.
Dude! That is all for now.
Thank you for reading.
Search for a plugin named “Pathless Category Links”. This will get rid of the “/category”. I imagine you can mod it to remove the “/pages”.
@ Bahamut
Thank you for your suggestion.
But it is too late.
Thanx.. I needed this plugin!
How will the code change if suppose instead of root index.php, I have a file named yshort.php residing in a template and I need to pass variables to this file and want to get it rewritten? Pls help in this case?
@ Navjot Singh
Read this article:
http://www.tanzilo.com/2008/11/23/flickr-gallery-in-wordpress-integration-with-falbum-plugin/
And try to use this plugin.
Then you can see how they are passing variables using GET method
Still having some trouble getting this right…very frustrating…uggggggggh!!!