LearnCreating Vanity URLs in PHP: Subfolders and Header Redirects

Randy Hoyt
writes on January 31, 2013

Share with your friends


Every page on a website has a unique web address that appears in the address bar when someone visits the page in a browser. These web addresses are known as URLs. (This stands for “uniform resource locator,” but that’s not too important.) This includes the domain name plus information like folder names and the file name for the specific page. Often, visitors to your site do not manually type in this web address; they arrive from a search engine or social media link, or they go to your home page and navigate to the specific page.

However, there are plenty of cases when you need to provide a link to a specific page that will be entered manually. Vanity URLs are extremely useful in these cases. A vanity URL is a short web address that redirects to another, longer one. You’ll often see these links in offline marketing material, like printed brochures or television commercials. Also, any web address that you might mention in an interview or on a podcast — or that you might tell a random stranger you meet in a coffee shop — needs to be easy to say out loud and easy to remember.

When I speak at a conference, I create a page on my website that contains the slides, links to any of the resources I mention, and a download of all the code I covered. I also create a vanity URL for that page that I can display on the slides and that I can easily tell conference attendees in person. For example, I created a page for my talk at WordCamp San Francisco last summer with this web address:


The vanity URL that I gave out at the conference had this shorter web address, based on the conference’s hash tag:


This vanity URL is much shorter, easier to say out loud, and easier to remember.

The easiest way to create a vanity URL on a web server running PHP is to use a subfolder with a header redirect.

Step 1: Create Subfolder
Using this approach, I would create a subfolder with the name wcsf in the main folder of my web server. Depending on your server configuration, that folder might be called public_html, www, htdocs, or something similar: it’s the folder on the web server where the home page file is located. If someone were to visit randyhoyt.com/wcsf at this point, the server would show them a directory listing indicating that the folder is empty — or it would show them a warning that they don’t have permission to see the directory listing.

Vanity URLs: Empty Directory Listing

Step 2: Create index.php File
Next, I would add a file into this subfolder. If I name that file index.php, my server will display that file instead of the directory listing at this web address. I’ll add the following code to that file:


    echo "Now can you hear me?";

In the browser, visiting the vanity URL will display this message.

Step 3: Code a Header Redirect
When someone visits this wcsf vanity URL, we want to redirect them to a different URL. We can achieve that by using a header redirect. In general, when someone enters a web address in a browser, the browser sends off a request to the web server asking for that page. The web server’s response to the browser has two pieces:

  • the content of the response, often HTML
  • data about the response, known as headers

These headers are not displayed in the browser, even if you view the source: they contain information about the response, the server, how long the browser should cache the result, whether or not the content is compressed in any way, and more. You’ll often hear these called HTTP headers: the browser and the server communicate using the hypertext transfer protocol (HTTP), and that protocol defines the format to be used for passing this data in headers.

The header value I need to use to redirect our vanity URL is “Location.” I don’t need to send any content or HTML back to the browser. Instead, I just tell the browser to look for the page at a different location. To achieve that, I can replace the echo statement I typed earlier and change the code in my index.php file to this:


    header("Location: http://randyhoyt.com/wordpress/subordinate-post-types/");

With this code in place, the vanity URL works when someone types it into a browser or follows a link to it.

Step 4: Change the Status Code
There is one additional header to consider when setting Location, primarily related to search engines. In most cases, the header includes a response code with a value of 200 that indicates the response was successful. There are a handful of other responses, including 404 (page not found) and 500 (internal server error). The response code 301 is relevant to header redirects; it indicates that the requested resource has moved permanently. With a 301 response, search engines will then know to index the page at the other end of the redirect — instead of at this vanity URL.

The following code in our index.php file will set the response code to 301 for the vanity URL:


    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://randyhoyt.com/wordpress/subordinate-post-types/");

Vanity URLs can be extremely useful, especially in offline marketing materials. A subfolder with a header redirect is the easiest way to get a vanity URL up and running on a web server running PHP.

18 Responses to “Creating Vanity URLs in PHP: Subfolders and Header Redirects”

  1. Thanks Randy for script :-)

  2. Rohan on May 18, 2013 at 7:49 am said:

    How do I redirect a Google search result to files in different sub-folders based on the pattern based on the search key word?

    EXAMPLE: Search in Google using the words ‘Sobha Habitech’. The resultant first search result after the ad is http://www.sobhadevelopers.com/projects/habitech/index.html

    It was a redirection purely based on the key word. So this is not a permanent redirect of the primary domain. It is condition based redirect which is based on the key word used in each situation. You can try another key word ‘Sobha Marvella’..

    Can you please help me out on how to accomplish this?

    Pls mail me at rohan.solank@gmail.com

  3. Thanks for another informative blog. Where else could
    I get that type of information written in such a perfect manner?

    I’ve a challenge that I’m simply now working on, and I have been on the look out
    for such info.

  4. Hello I am so excited I found your website, I really found you
    by error, while I was researching on Askjeeve for something else, Regardless I
    am here now and would just like to say thanks for a marvelous post and a all round exciting blog (I
    also love the theme/design), I don’t have time to read it
    all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the fantastic

  5. Appreciating the time and effort you put into your site and in depth information you present.
    It’s good to come across a blog every once in a while that isn’t the same old rehashed material.
    Wonderful read! I’ve bookmarked your site and I’m adding your RSS feeds
    to my Google account.

  6. Hi mates, pleasant article and nice arguments commented here, I am truly enjoying by these.

  7. This is my first time visit at here and i am really impressed to read everthing at
    single place.

  8. Awesome! Its actually awesome article, I have got much clear idea concerning from this piece of writing.

  9. If some one wishes expert view about blogging and site-building afterward i suggest him/her to pay a quick visit this website, Keep up the nice work.

  10. Excellent beat ! I would like to apprentice at the same time as you amend
    your web site, how could i subscribe for a blog website?
    The account helped me a applicable deal. I have been tiny bit familiar of this your broadcast provided shiny clear concept

  11. Helpful information. Fortunate me I found yoսr site byy chance, аnd
    ӏ’m stunned ԝhy tuis twist оf fate didn’thappened
    іn advance! I bookmarked іt.

  12. Delight in a cup of tea with this particular audio encouraged mug from Dunoon. Made by Kate Mawdsley and A part of a set of 3 mugs, the fine bone china mug options an array of musical instruments, from the cello to some bag pipes. Massive chunky and really cozy to consume from, Nevis form good bone china mugs have a generous capacity of 480ml. All types are both dishwasher and microwave Risk-free, except Those people showcasing gold embellishment.

  13. Just wish to say your article is as amazing. The clearness in your post is simply great and i
    could assume you’re an expert on this subject. Well with your permission allow me to grab
    your feed to keep updated with forthcoming post. Thanks a million and please
    continue the gratifying work.

  14. In playing field of Italian Leather Bags are decidedly in requiring
    because of the improved and the stylish look. The design concept of Gucci tends
    to be simple and extensive just like the Pelham one-shoulder handbag I am carrying on. Inside you can see durable padding, making it capable of protecting documents, flash drives, CDs, and other electronic items.

  15. Do you need to have any other files in the directory? I have an index.php file in my vanity subdirectory that has exactly this code in it, but I keep getting a 403 error and I can’t figure out why.

  16. It’s very trouble-free to find out any matter on web as compared to textbooks, as I found this post at this site.

  17. What’s up, the whole thing is going sound here and ofcourse every one is sharing information, that’s in fact excellent, keep up writing.

Leave a Reply

Want to learn more about PHP?

Learn how to create dynamic websites using the back-end programming language, PHP.

Learn more