Pretty URLs: Pretty Easy!

Share this article

Back when I wrote the third edition of Build Your Own Database Driven Web Site Using PHP & MySQL, it was still acceptable (though certainly unfashionable) to lump all of the code together to generate a web page into a single file, and then use a URL naming that file to access the page.

PHP has moved on. These days, a developer that builds web sites that way is unlikely to gain a job (except perhaps one that pays poorly). Developers are expected to organize their code into multiple files, and URLs containing file names like index.php are definitely on their way out.

Experienced developers will use finely-crafted frameworks and web server configurations to convert so-called pretty URLs like http://www.example.com/thingy/. No “.php” advertising the fact that the site is built in PHP, no specific filename at all. URLs like this one are designed to be as simple and memorable as possible.

For the fourth edition of the book, I wanted developers to make a habit of this earlier, without forcing them to fiddle with their server configuration, or learn a complex PHP framework.

The easiest possible way I could find to produce pretty URLs was to take advantage of a feature built into every PHP-equipped web server out there: a URL that points to a directory on the server will automatically run a PHP script named index.php sitting in that directory.

With that in mind, here’s how all of the examples in the book from Chapter 3 onward are structured:

  • A folder
    Everything goes inside a folder, whose name is the only visible part in the URL. So if the example displays “thingies”, you would start by creating a folder named thingies, and the URL of the example would be http://www.example.com/thingies/
  • A controller, index.php
    When a browser requests the example’s URL, the server will automatically run the index.php script contained in the folder. This file is called the controller, and contains all the logic for handling browser requests and performing tasks in response.
  • Include files, like db.inc.php
    Pieces of code that will be useful in more than one example are pulled out of index.php and placed in separate PHP files. This is basic stuff for PHP veterans, but it’s a valuable coding technique that was covered much later in the third edition, in Chapter 12. Now it’s in Chapter 3.
  • Template files, like thingy.html.php
    Once the controller (with the assistance of one or more include files) does all the work, it must prepare a web page to send back to the browser in response. Rather than slopping all the HTML code into index.php (which is what we did all the way through the third edition of the book), the fourth edition uses a separate file for the HTML code for each type of page. These template files consist mostly of pure HTML; the only PHP code they contain is there to output the values of PHP variables created by the controller. The code in these files is kept very simple, so that they can be handed off to designers who may be unfamiliar with PHP at all. Also, using separate files makes it easy to generate several different types of page (that is, serveral different template files) from a single controller.

All the little changes in approach like this one are what really makes me proud of Build Your Own Database Driven Web Site Using PHP & MySQL, 4th Edition. I used to describe the third edition of this book as giving beginners “just enough rope to hang themselves with.”

With the fourth edition, beginners can breeze past the self-strangulation stage — hopefully straight into a well-paying career as a PHP developer!

I bet you’re wondering where the code is, right? To see how the “pretty URL” technique described above looks in code, be sure to check out Chapter 3 when we publish it on Thursday. Or if you’re unable to wait, grab all four chapters in PDF format today — free of charge.

Frequently Asked Questions about Pretty URLs

What is the significance of Pretty URLs in SEO?

Pretty URLs, also known as clean URLs, play a significant role in Search Engine Optimization (SEO). They are designed to be readable, concise, and memorable, which makes them user-friendly and easy to share. They also help search engines understand the content of the page better, which can improve the page’s ranking in search results. Moreover, pretty URLs can include keywords that are relevant to the page’s content, further enhancing SEO.

How can I convert my dynamic URLs into Pretty URLs?

Converting dynamic URLs into pretty URLs involves rewriting the URLs using a module like mod_rewrite for Apache servers. This module allows you to translate complex URLs into user-friendly and search engine-friendly URLs. You can also use plugins or extensions if you’re using a CMS like WordPress or Joomla. These tools automatically generate pretty URLs for your pages.

Are there any potential issues with Pretty URLs?

While pretty URLs offer many benefits, they can also present some challenges. For instance, if not implemented correctly, they can lead to duplicate content issues. This happens when the same content is accessible through multiple URLs. To avoid this, it’s important to use canonical tags to tell search engines which version of the URL to index.

How do Pretty URLs affect user experience?

Pretty URLs can significantly enhance the user experience. They are easier to read, understand, and remember compared to dynamic URLs. This makes it easier for users to navigate your site, share your content, and return to your pages in the future.

Can I use special characters in Pretty URLs?

It’s generally recommended to avoid using special characters in pretty URLs. These characters can cause issues with certain browsers and search engines. Instead, use hyphens to separate words in your URLs. This makes them easier to read and more SEO-friendly.

How do I implement Pretty URLs in a CMS?

Most content management systems (CMS) have built-in features or plugins that allow you to implement pretty URLs. For example, in WordPress, you can change your URL structure in the Permalinks settings. In Joomla, you can use the built-in SEF URLs feature.

What is the difference between absolute and relative Pretty URLs?

Absolute URLs include the full path to a page, including the domain name. For example, “https://www.example.com/page“. On the other hand, relative URLs only include the path to the page from the current directory, like “/page”. Both can be used in pretty URLs, but absolute URLs are generally preferred for SEO purposes.

How do Pretty URLs work with subdirectories?

Pretty URLs can include subdirectories, which represent different levels or categories on your website. For example, “https://www.example.com/category/page“. This structure can help users and search engines understand the hierarchy and organization of your site.

Can Pretty URLs include parameters?

While it’s possible to include parameters in pretty URLs, it’s generally not recommended. Parameters can make URLs look complex and less user-friendly. If you need to include parameters, consider using a URL rewriting tool to hide them.

How do I test my Pretty URLs?

You can test your pretty URLs by navigating to them in your web browser. Make sure they lead to the correct pages and that all links on your site are updated to use the new URLs. You can also use online tools to check for broken links and ensure your URLs are properly formatted.

Kevin YankKevin Yank
View Author

Kevin Yank is an accomplished web developer, speaker, trainer and author of Build Your Own Database Driven Website Using PHP & MySQL and Co-Author of Simply JavaScript and Everything You Know About CSS is Wrong! Kevin loves to share his wealth of knowledge and it didn't stop at books, he's also the course instructor to 3 online courses in web development. Currently Kevin is the Director of Front End Engineering at Culture Amp.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week