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:
{code type=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’);
?>
{/code}
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:
{code type=PHP}
$group_name = $_GET['group_name'];
$group_name = str_replace('-', ' ', $group_name);
?>
(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.
{code type=HTML}
# 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]
{/code}
Finally my (dot)htaccess file looked like this:
{code type=HTML}
# BEGIN WordPress
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]
# END WordPress
{/code}
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!!!
Hello,
We are trying to figure out a way of having the option to use about.php for the about page and in general having the control to use COMPLETELY CUSTOM URLs for each post or page.
What would be the solution to finding this ?
Alexander
@ Alexander
may be you can use some plugins developed for this purpose if available
Hi,
I have been searching for a solution for a long time and see that there is really nothing available.
Are you available to work with us to solve this problem?
Hey, that is a great article!
I wonder if you offer individual help on WordPress/php problems?
I want to create a rewrite rule or plugin that makes permalinks to articles look like .html files – to look like actual links to html-pages. And i want to make the links to embedded images in articles look like they come not from the single /flies directory, but from a directory for each article.
Could you help with that and how much would you charge?
Robert
Hey Robert,
Just so you know the rewrite rules have changed a little since this article was written and, actually, some functions have been deprecated.
Not to take anything away from our fabulous developer and writer of this article but in the event that you will need this implemented please contact me at alexander(at)eligonsystems.ca
Good luck,
Alexander