Dec 17, 2008 - PHP, Wordpress    11 Comments

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:

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:

This was half the way winning the battle.

But why? Because the following lines should solve my problem.

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.

Finally my (dot)htaccess file looked like this:

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.

11 Comments

  • 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

Got anything to say? Go ahead and leave a comment!