Web Design Forum: TUTORIAL: Custom URL with PHP (Permalinks) - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: Custom URL with PHP (Permalinks) An Alternative to .htaccess Rate Topic: -----

#1 User is offline   Daniel 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 128
  • Joined: 09-September 07
  • Reputation: 0
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

  Posted 25 June 2008 - 12:17 AM

Introduction

When using PHP generated pages, there is one common problem: it is dynamically loaded using set variables. For example, if you want to read the latest blog entry on a blog with any URL structure, you will find http://arandomblog.com/?entry=1. This structure is easy to use but when it comes to search engine and general public use, you’ll find that it will not have many benefits. Instead, you’ll often find http://arandomblog/randomentry/. Why? It looks more professional, it is more usable, and it will give you keyword placement.

There is an easy way to do so using .htaccess mod_rewrite. However, for whatever reasons, be it free hosting or server limitations, you might not have access to the .htaccess file or may not want to. I do recommend using mod_rewrite for this task if at all possible. I will not be discussing it further here as it is outside the scope of this article. Moving on…

Test your Server

If you want to still have custom URL structures without .htaccess, one approach you can use is through PHP. On some servers, is is possible to have a structure such as http://www.nexterous...w-nexterouscom/. Notice the key component of this structure; this is the the blog.php. As long as you have a valid file and you do not mind that little setback, you are good to move on.

Before we actually begin, please test to make sure your server supports this method. Upload a PHP file that just echoes ‘Hello World’ and go to it in the URL. When you input the URL, add to it a few divisions. An example URL would be http://www.mysite.co...age.php/1/hello. If there is no error, you are good to go.

Breaking Down the URL

For this article, the following structure will be used (siteurl/blog.php/entryid/entryslug). To begin, you want to break up the URL into parts so that you can analyze each part and continue. Using PHP’s parse_url function, you get the following code:

$url = parse_url($_SERVER['PATH_INFO'], PHP_URL_PATH);
$url = explode(’/', $url);


The above code is relatively simple. It parses the URL using only the entry path (/entryid/entryslug) and then divides it into parts separated by the dashes. The variable $url is an array with at least 3 parts: a black row, a row with the an integer, and a row with the slug of the title. Now having that data, you can call the entry from the database using the ID. We want to add one more effect.

SEO and Titles

Now, let’s say that all is working fine. You grab the ID from the $url array and call the entry and it loads perfectly. However, for some reason, either you entered the URL wrong into the HTML or your visitor switched the URL. Now the title is changed. For search engines and consistency, you don’t want that to happen. What you do in this case is you have a set standard for creating the slug. After you parse the URL using the code above, you would create the slug on the spot for that entry using your standard and then compare that to the slug you received in the URL. If they are not equal, you would do a header redirect in PHP where the slug you created on the spot would be used to redirect. Let me show you code directly from my site.


# Get the latest blog entry
     $entry = $api->get_post($entryid);

# Auto-correct URL
     if($urltitle != $entry['rawslug']){
          $header = "Location: {$entry['slug']}";
          header($header);
          exit();
     }


First, I get all the post information for the entry using my custom API (included in the API is the slug creating function). I then do a comparison to check if the $urltitle (set from the parse url code) isn’t equal to the $entry['rawslug'] created from my API. If that is true, then it redirects to the $entry['slug'] created from the API (which is just the full URL). This process is very quick and you will see very limited, if any, performance slow from this process.

Summary

To summarize, what we did in this article was to set a custom URL structure using PHP. This will benefit those who cannot use .htaccess either through server limitations or difficulty. We first parse the URL and explode it into an array. Using the array data, we then set different variables from it. To keep URL’s consistent, we compare the slug entered in the URL with that of the database. If it was not equal, we would redirect it to the correct URL.

Posted
Originally posted on my site: http://www.nexterous...php-custom-url/
Written for the release of version 3 of my site.
More articles on web development, PHP and coding will follow soon.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

4 User(s) are reading this topic
0 members, 4 guests, 0 anonymous users