Menu Search
Jump to the content X X
Smashing Conf New York

We use ad-blockers as well, you know. We gotta keep those servers running though. Did you know that we publish useful books and run friendly conferences — crafted for pros like yourself? E.g. upcoming SmashingConf New York, dedicated to smart front-end techniques and design patterns.

Introduction To URL Rewriting


Many Web companies spend hours and hours agonizing over the best domain names for their clients. They try to find a domain name that is relevant and appropriate, sounds professional yet is distinctive, is easy to spell and remember and read over the phone, looks good on business cards and is available as a dot-com.

Or else they spend thousands of dollars to purchase the one they really want, which just happened to be registered by a forward-thinking and hard-to-find squatter in 1998.

They go through all that trouble with the domain name but neglect the rest of the URL, the element after the domain name. It, too, should be relevant, appropriate, professional, memorable, easy to spell and readable. And for the same reasons: to attract customers and improve in search ranking.

Fortunately, there is a technique called URL rewriting that can turn unsightly URLs into nice ones — with a lot less agony and expense than picking a good domain name. It enables you to fill out your URLs with friendly, readable keywords without affecting the underlying structure of your pages.

This article covers the following:

  1. What is URL rewriting?
  2. How can URL rewriting help your search rankings?
  3. Examples of URL rewriting, including regular expressions, flags and conditionals;
  4. URL rewriting in the wild, such as on Wikipedia, WordPress and shopping websites;
  5. Creating friendly URLs;
  6. Changing pages names and URLs;
  7. Checklist and troubleshooting.

What Is URL Rewriting? Link

If you were writing a letter to your bank, you would probably open your word processor and create a file named something like lettertobank.doc. The file might sit in your Documents directory, with a full path like C:WindowsusersjulieDocumentslettertobank.doc. One file path = one document.

Similarly, if you were creating a banking website, you might create a page named page1.html, upload it, and then point your browser to One URL = one resource. In this case, the resource is a physical Web page, but it could be a page or product drawn from a CMS.

URL rewriting changes all that. It allows you to completely separate the URL from the resource. With URL rewriting, you could have taking the user to …/page1.html or to …/about-us/ or to …/about-this-website-and-me/ or to …/youll-never-find-out-about-me-hahaha-Xy2834/. Or to all of these. It’s a bit like shortcuts or symbolic links on your hard drive. One URL = one way to find a resource.

With URL rewriting, the URL and the resource that it leads to can be completely independent of each other. In practice, they’re usually not wholly independent: the URL usually contains some code or number or name that enables the CMS to look up the resource. But in theory, this is what URL rewriting provides: a complete separation.

How Does URL Rewriting Help? Link

Can you guess what this Web page sells?

B&Q went to all the trouble and expense of acquiring and implementing a stock controlled e-commerce website, but left its URLs indecipherable. If you guessed “brown guttering,” you might want to considering playing the lottery.

Even when you search directly for this “miniflow gutter brown” on Google UK, B&Q’s page comes up only seventh in the organic search results, below much smaller companies, such as a building supplier with a single outlet in Stirlingshire. B&Q has 300+ branches and so is probably much bigger in budget, size and exposure, so why is it not doing as well for this search term? Perhaps because the other search results have URLs like…; that is, the URL itself contains the words in the search term.


Almost all of these results on Google have the search term in their URLs (highlighted in green). The one at the bottom does not.

Looking at the URL from B&Q, you would (probably correctly) assume that a file named nav.jsp within the directory /diy/jsp/bq/ is used to display products when given their ID number, 11577676 in this case. That is the resource intimately tied to this URL.

So, how would B&Q go about turning this into something more recognizable, like, without restructuring its whole website? The answer is URL rewriting.

Another way to look at URL rewriting is like a thin layer that sits on top of a website, translating human- and search-engine-friendly URLs into actual URLs. Doing it is easy because it requires hardly any changes to the website’s underlying structure — no moving files around or renaming things.

URL rewriting basically tells the Web server that
/products/miniflow-gutter-brown/11577676 should show the Web page at: /diy/jsp/bq/nav.jsp?action=detail&fh_secondid=11577676,
without the customer or search engine knowing about it.

Many factors (or “signals”), of course, determine the search ranking for a particular term, over 200 of them according to Google. But friendly and readable URLs are consistently ranked as one of the most important1 of those factors. They also help humans to quickly figure out what a page is about.

The next section describes how this is done.

How To Rewrite URLs Link

Whether you can implement URL rewriting on a website depends on the Web server. Apache usually comes with the URL rewriting module, mod_rewrite, already installed. The set-up is very common and is the basis for all of the examples in this article. ISAPI Rewrite2 is a similar module for Windows IIS but requires payment (about $100 US) and installation.

The Simplest Case Link

The simplest case of URL rewriting is to rename a single static Web page, and this is far easier than the B&Q example above. To use Apache’s URL rewriting function, you will need to create or edit the .htaccess file in your website’s document root (or, less commonly, in a subdirectory).

For instance, if you have a Web page about horses named Xu8JuefAtua.htm, you could add these lines to .htaccess:

RewriteEngine On
RewriteRule   horses.htm   Xu8JuefAtua.htm

Now, if you visit, you’ll actually be shown the Web page Xu8JuefAtua.htm. Furthermore, your browser will remain at horses.htm, so visitors and search engines will never know that you originally gave the page such a cryptic name.

Introducing Regular Expressions Link

In URL rewriting, you need only match the path of the URL, not including the domain name or the first slash. The rule above essentially tells Apache that if the path contains horses.htm, then show the Web page Xu8JuefAtua.htm. This is slightly problematic, because you could also visit, and it would still work. So, what we really need is this:

RewriteEngine On
RewriteRule   ^horses.htm$   Xu8JuefAtua.htm

The ^horses.htm$ is not just a search string, but a regular expression3, in which special characters — such as ^ . + * ? ^ ( ) [ ] { } and $ — have extra significance. The ^ matches the beginning of the URL’s path, and the $ matches the end. This says that the path must begin and end with horses.htm. So, only horses.htm will work, and not reallyfasthorses.htm or horses.html. This is important for search engines like Google, which can penalize what it views as duplicate content74 — identical pages that can be reached via multiple URLs.

Without File Endings Link

You can make this even better by ditching the file ending altogether, so that you can visit either or

RewriteEngine On
RewriteRule   ^horses/?$   Xu8JuefAtua.html  [NC]

The ? indicates that the preceding character is optional. So, in this case, the URL would work with or without the slash at the end. These would not be considered duplicate URLs by a search engine, but would help prevent confusion if people (or link checkers) accidentally added a slash. The stuff in brackets at the end of the rule gives Apache some further pointers. [NC] is a flag that means that the rule is case insensitive, so would also work.

Wikipedia Example Link

We can now look at a real-world example. Wikipedia appears to use URL rewriting, passing the title of the page to a PHP file. For instance…

… is rewritten to:

This could well be implemented with an .htaccess file, like so:

RewriteEngine On
#Look for the word "wiki" followed by a slash, and then the article title
RewriteRule   ^wiki/(.+)$   w/index.php?title=$1   [L]

The previous rule had /?, which meant zero or one slashes. If it had said /+, it would have meant one or more slashes, so even would have worked. In this rule, the dot (.) matches any character, so .+ matches one or more of any character — that is, essentially anything. And the parentheses — ( ) — ask Apache to remember what the .+ is. The rule above, then, tells Apache to look for wiki/ followed by one or more of any character and to remember what it is. This is remembered and then rewritten as $1. So, when the rewriting is finished, wiki/Barack_obama becomes w/index.php?title=Barack_obama

Thus, the page w/index.php is called, passing Barack_obama as a parameter. The w/index.php is probably a PHP page that runs a database lookup — like SELECT * FROM articles WHERE title='Barack obama' — and then outputs the HTML.


You can also view Wikipedia entries directly, without the URL rewriting.

Comments and Flags Link

The example above also introduced comments. Anything after a # is ignored by Apache, so it’s a good idea to explain your rewriting rules so that future generations can understand them. The [L] flag means that if this rule matches, Apache can stop now. Otherwise, Apache would continue applying subsequent rules, which is a powerful feature but unnecessary for all but the most complex rule sets.

Implementing the B&Q Example Link

The recommendation for B&Q above could be implemented with an .htaccess file, like so:

RewriteEngine On
#Look for the word "products" followed by slash, product title, slash, id number
RewriteRule  ^products/.*/([0-9]+)$   diy/jsp/bq/nav.jsp?action=detail&fh_secondid=$1 [NC,L]

Here, the .* matches zero or more of any character, so nothing or anything. And the [0-9] matches a single numerical digit, so [0-9]+ matches one or more numbers.

The next section covers a couple of more complex conditional examples. You can also read the Apache rewriting guide5 for much more information on all that URL rewriting has to offer.

Conditional Rewriting Link

URL rewriting can also include conditions and make use of environment variables. These two features make for an easy way to redirect requests from one domain alias to another. This is especially useful if a website changes its domain, from to for example.

Domain Forwarding Link

Most domain registrars allow for domain forwarding, which redirects all requests from one domain to another domain, but which might send requests for to the home page at and not to You can achieve this with URL rewriting instead:

RewriteEngine On
RewriteCond   %{HTTP_HOST}   !^$         [NC]
RewriteRule   (.*) $1  [L,R=301]

The second line in this example is a RewriteCond, rather than a RewriteRule. It is used to compare an Apache environment variable on the left (such as the host name in this case) with a regular expression on the right. Only if this condition is true will the rule on the next line be considered.

In this case, %{HTTP_HOST} represents, the host (i.e. domain) that the browser is trying to visit. The ! means “not.” This tells Apache, if the host does not begin and end with, then remember and rewrite zero or more of any character to$1. This converts to And it will work for all other aliases as well, like and

The flag [R=301] is very important. It tells Apache to do a 301 (i.e. permanent) redirect. Apache will send the new URL back to the browser or search engine, and the browser or search engine will have to request it again. Unlike all of the examples above, the new URL will now appear in the browser’s location bar. And search engines will take note of the new URL and update their databases. [R] by itself is the same as [R=302] and signifies a temporary redirect.

File Existence and WordPress Link

Smashing Magazine runs on the popular blogging software WordPress. WordPress enables the author to choose their own URL, called a “slug.” Then, it automatically prepends the date, such as In your pre-URL rewriting days, you might have assumed that Smashing Magazine’s Web server was actually serving up a file located at …/2011/09/05/getting-started-with-the-paypal-api/index.html. In fact, WordPress uses URL rewriting extensively.


WordPress enables the author to choose their own URL for an article.

WordPress’ .htaccess file looks like this:

RewriteEngine On
RewriteBase /  
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

The -f means “this is a file” and -d means “this is a directory.” This tells Apache, if the requested file name is not a file, and the requested file name is not a directory, then rewrite everything (i.e. any path containing any character) to the page index.php. If you are requesting an existing image or the log-in page wp-login.php, then the rule is not triggered. But if you request anything else, like /2011/09/05/getting-started-with-the-paypal-api/, then the file index.php jumps into action.

Internally, index.php (probably) looks at the environment variable $_SERVER['REQUEST_URI'] and extracts the information that it needs to find out what it is looking for. This gives it even more flexibility than Apache’s rewrite rules and enables WordPress to mimic some very sophisticated URL rewriting rules. In fact, when administering a WordPress blog, you can go to Settings → Permalink on the left side, and choose the type of URL rewriting that you would like to mimic.


WordPress’ permalink settings, letting you choose the type of URL rewriting that you would like to mimic.

Rewriting Query Strings Link

If you are hired to recreate an existing website from scratch, you might use URL rewriting to redirect the 20 most popular URLs on the old website to the locations on the new website. This could involve redirecting things like prod.php?id=20 to products/great-product/2342, which itself gets redirected to the actual product page.

Apache’s RewriteRule applies only to the path in the URL, not to parameters like id=20. To do this type of rewriting, you will need to refer to the Apache environment variable %{QUERY_STRING}. This can be accomplished like so:

RewriteEngine On
RewriteCond   %{QUERY_STRING}           ^id=20$                   
RewriteRule   ^prod.php$             ^products/great-product/2342$      [L,R=301]
RewriteRule   ^products/(.*)/([0-9]+)$  ^productview.php?id=$1             [L]

In this example, the first RewriteRule triggers a permanent redirect from the old website’s URL to the new website’s URL. The second rule rewrites the new URL to the actual PHP page that displays the product.

Examples Of URL Rewriting On Shopping Websites Link

For complex content-managed websites, there is still the issue of how to map friendly URLs to underlying resources. The simple examples above did that mapping by hand, manually associating a URL like horses.htm with the file or resource Xu8JuefAtua.htm. Wikipedia looks up the resource based on the title, and WordPress applies some complex internal rule sets. But what if your data is more complex, with thousands of products in hundreds of categories? This section shows the approach that Amazon and many other shopping websites take.

If you’ve ever come across a URL like this on Amazon,, you might have assumed that Amazon’s website has a subdirectory named /High-Voltage-AC-DC/dp/ that contains a file named B00008AJL3.

This is very unlikely. You could try changing the name of the top-level “directory” and you would still arrive on the same page,

The bit at the end is what really matters. Looking down the page, you’ll see that B00008AJL3 is this AC/DC album’s ASIN (Amazon Standard Identification Number). If you change that, you’ll get a “Page not found” or an entirely different product:

The /dp/ also matters. Changing this leads to a “Page not found.” So, the B00008AJL3 probably tells Amazon what to display, and the dp tells the website how to display it. This is URL rewriting in action, with the original URL possibly ending up getting rewritten to something like:

Features of an Amazon URL Link

This introduces some important features of Amazon’s URLs that can be applied to any website with a complex set of resources. It shows that the URL can be automatically generated and can include up to three parts:

  1. The words
    In this case, the words are based on the album and artist, and all non-alphanumeric characters are replaced. So, the slash in AC/DC becomes a hyphen. This is the bit that helps humans and search engines.
  2. An ID number
    Or something that tells the website what to look up, such as B00008AJL3.
  3. An identifier
    Or something that tells the website where to look for it and how to display it. If dp tells Amazon to look for a product, then somewhere along the line, it probably triggers a database statement such as SELECT * FROM products WHERE id='B00008AJL3'.

Other Shopping Examples Link

Many other shopping websites have URLs like this. In the list below, the ID number and (suspected) identifier are in bold:


A significant benefit of this type of URL is that the actual words can be changed, as shown below. As long as the ID number stays the same, the URL will still work. So products can be renamed without breaking old links. More sophisticated websites (like Ciao above) will redirect the changed URL back to the real one and thus avoid creating the appearance of duplicate content (see below for more on this topic).


Websites that use URL rewriting are more flexible with their URLs — the words can change but the page will still be found.

Friendly URLs Link

Now you know how to map nice friendly URLs to their underlying Web pages, but how should you create those friendly URLs in the first place?

If we followed the current advice, we would separate words with hyphens rather than underscores6 and capitalize consistently. Lowercase might be preferable because most people search in lowercase. Punctuation such as dots and commas should also be turned into hyphens, otherwise they would get turned into things like %2C, which look ugly and might break the URL when copied and pasted. You might want to remove apostrophes and parentheses entirely for the same reason.

Whether to replace accented characters is debatable. URLs with accents (or any non-Roman characters) might look bad or break when rendered in a different character format. But replacing them with their non-accented equivalents might make the URLs harder for search engines to find (and even harder if replaced with hyphens). If your website is for a predominately French audience, then perhaps leave the French accents in. But substitute them if the French words are few and far between on a mainly English website.

This PHP function succinctly handles all of the above suggestions:

function GenerateUrl ($s) {
  //Convert accented characters, and remove parentheses and apostrophes
  $from = explode (',', "ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u,(,),[,],'");
  $to = explode (',', 'c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u,,,,,,');
  //Do the replacements, and convert all other non-alphanumeric characters to spaces
  $s = preg_replace ('~[^wd]+~', '-', str_replace ($from, $to, trim ($s)));
  //Remove a - at the beginning or end and make lowercase
  return strtolower (preg_replace ('/^-/', '', preg_replace ('/-$/', '', $s)));

This would generate URLs like this:

echo GenerateUrl ("Pâtisserie (Always FRESH!)"); //returns "patisserie-always-fresh"

Or, if you wanted a link to a $product variable to be pulled from a database:

$product = array ('title'=>'Great product', 'id'=>100);
echo '<a href="' . GenerateUrl ($product['title']) . '/' . $product['id'] . '">';
echo $product['title'] . '</a>';

Changing Page Names Link

Search engines generally ignore duplicate content74 (i.e. multiple pages with the same information). But if they think they are being manipulated, search engines will actively penalize the website, so avoid this where possible. Google recommends using 301 redirects to send users from old pages to new ones.

When a URL-rewritten page is renamed, the old URL and new URL should both still work. Furthermore, to avoid any risk of duplication, the old URL should automatically redirect to the new one, as WordPress does.

Doing this in PHP is relatively easy. The following function looks at the current URL, and if it’s not the same as the desired URL, it redirects the user:

function CheckUrl ($s) {
  // Get the current URL without the query string, with the initial slash
  $myurl = preg_replace ('/?.*$/', '', $_SERVER['REQUEST_URI']);
  //If it is not the same as the desired URL, then redirect
  if ($myurl != "/$s") {Header ("Location: /$s", true, 301); exit;}

This would be used like so:

$producturl = GenerateUrl ($product['title']) . '/' . $product['id'];
CheckUrl ($producturl); //redirects the user if they are at the wrong place

If you would like to use this function, be sure to test it in your environment first and with your rewrite rules, to make sure that it does not cause any infinite redirects. This is what that would look like:


This is what happens when Google Chrome visits a page that redirects to itself.

Checklist And Troubleshooting Link

Use the following checklist to implement URL rewriting.

1. Check That It’s Supported Link

Not all Web servers support URL rewriting. If you put up your .htaccess file on one that doesn’t, it will be ignored or will throw up a “500 Internal Server Error.”

2. Plan Your Approach Link

Figure out what will get mapped to what, and how the correct information will still get found. Perhaps you want to introduce new URLs, like my-great-product/p/123, to replace your current product URLs, like product.php?id=123, and to substitute new-category/c/12 for category.php?id=12.

3. Create Your Rewrite Rules Link

Create an .htaccess file for your new rules. You can initially do this in a /testing/ subdirectory and using the [R] flag, so that you can see where things go:

RewriteEngine On
RewriteRule   ^.+/p/([0-9]+)   product.php?id=$1    [NC,L,R]
RewriteRule   ^.+/c/([0-9]+)   category.php?id=$1    [NC,L,R]

Now, if you visit, you should be sent to You’ll get a “Page not found” because product.php is not in your /testing/ subdirectory, but at least you’ll know that your rules work. Once you’re satisfied, move the .htaccess file to your document root and remove the [R] flag. Now should work.

4. Check Your Pages Link

Test that your new URLs bring in all the correct images, CSS and JavaScript files. For example, the Web browser now believes that your Web page is named 123 in a directory named my-great-product/p/. If the HTML refers to a file named images/logo.jpg, then the Web browser would request the image from and would come up with a “File not found.”

You would need to also rewrite the image locations or make the references absolute (like <img src="/images/logo.jpg"/>) or put a base href at the top of the <head> of the page (<base href="/product.php"/>). But if you do that, you would need to fully specify any internal links that begin with # or ? because they would now go to something like product.php#details.

5. Change Your URLs Link

Now find all references to your old URLs, and replace them with your new URLs, using a function such as GenerateUrl to consistently create the new URLs. This is the only step that might require looking deep into the underlying code of your website.

6. Automatically Redirect Your Old URLs Link

Now that the URL rewriting is in place, you probably want Google to forget about your old URLs and start using the new ones. That is, when a search result brings up product.php?id=20, you’d want the user to be visibly redirected to my-great-product/p/123, which would then be internally redirected back to product.php?id=20.

This is the reverse of what your URL rewriting already does. In fact, you could add another rule to .htaccess to achieve this, but if you get the rules in the wrong order, then the browser would go into a redirect loop.

Another approach is to do the first redirect in PHP, using something like the CheckUrl function above. This has the added advantage that if you rename the product, the old URL will immediately become invalid and redirect to the newest one.

7. Update and Resubmit Your Site Map Link

Make sure to carry through your new URLs to your site map, your product feeds and everywhere else they appear.

Conclusion Link

URL rewriting is a relatively quick and easy way to improve your website’s appeal to customers and search engines. We’ve tried to explain some real examples of URL rewriting and to provide the technical details for implementing it on your own website. Please leave any comments or suggestions below.


Footnotes Link

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
SmashingConf New York

Hold on, Tiger! Thank you for reading the article. Did you know that we also publish printed books and run friendly conferences – crafted for pros like you? Like SmashingConf New York, on June 14–15, with smart design patterns and front-end techniques.

↑ Back to top Tweet itShare on Facebook

Paul Tero is an experienced PHP programmer and server administrator. He developed the Stockashop ecommerce system in 2005 for Sensable Media. He now works part-time maintaining and developing Stockashop, and the rest of the time freelancing from a corner of his living room, and sleeping, eating, having fun, etc. He has also written numerous other open sourcish scripts and programs.

  1. 1

    Julio Di Nicola

    November 2, 2011 4:48 am

    Great!! This is exactly what I was looking for yesterday for a new project.

    I’m gonna read it very carefully…


  2. 2

    One thing to also remember is to set canonical tags on a page. For example, the Barack Obama page has a tag that reads:

    This tells search engines that while the page can be accessed via and, the two pages are actually the same page and are not duplicates made for SEO purposes which may result in penalties from the search engines.

    • 3

      Agustin Giannastasio

      November 4, 2011 8:23 pm

      Hi John, this SEO issue (duplicated content) is avoided by this line:

      It tells which URL is preferred, so Google will takes only this URL to rank on search results ;)

  3. 4

    Excellent! This couldn’t have come at a better time.

  4. 5

    Manuel Garcia

    November 2, 2011 5:13 am

    Wow. This post seems like a book for me, full of knowledge. I was really amazed. Great post.

  5. 6

    Awesome :)
    It is a short, complete guide to URL rewriting for *nix server environment.
    Thanks Paul.

  6. 7

    Ashutosh Nigam

    November 2, 2011 5:36 am

    this is really an awesome post ……thanks for sharing

  7. 8

    Great time for an article like this. Thanks for the indepth look.

  8. 9


    //Convert accented characters, and remove parentheses and apostrophes
    $from = explode (‘,’, “ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u,(,),[,],'”);
    $to = explode (‘,’, ‘c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u,,,,,,’);

  9. 10

    awesome! thank you!

  10. 11

    Larry Sanborn

    November 2, 2011 7:28 am

    Just a note for those of us who don’t live in a LAMP world…

    IIS 7 (since Win Server 2008) has had a free URLRewrite module that I use quite successfully.

    ISAPI Rewrite is third party and intended for IIS6 (think Windows 2003)

  11. 12

    “File Existence and WordPress: Smashing Magazine runs on the popular blogging software WordPress.” …FYI, WordPress is a CMS, not just blogging software. Thanks for the great article!

  12. 13

    Marius de Beer

    November 2, 2011 11:00 am

    Thank you so much – this is probably the best written article I’ve come across on URL rewriting. Definitely gonna bookmark it for further study.

    Give that man a Bell’s !

  13. 14

    Wow, just wow!
    Just another very good reason why Smashing is a top notch resource.

  14. 15

    Excellent tutorial I’ve been rewriting urls for a few years now my way isn’t quite as elegant as the GenerateUrl function, well not anymore :)

    I prefer to add the converted url to a database then it can be used across the project.

    • 16

      I found this all confusing until you explained everything and made it seem so easy. Thanks. This is a great article and will help a ton!

  15. 17

    David Fregoli

    November 2, 2011 3:36 pm

    oh man, I’ve been striving all day long with htaccess at work today, read this too late!
    tomorrow I’ll finish it up nicely thank to you ^^

  16. 18

    Sebastiaan Stok

    November 2, 2011 6:59 am

    Two little tips.

    If the data is not changing its better make an array of it only once, instead of exploding something that is basically never changing.

    In the regex ‘.*’ is to captive, its better to make it more explicit like ‘[^/]+’.
    Or else the ‘/’ may get captured to.

    This to can be improved:
    return strtolower (preg_replace (‘/^-/’, ”, preg_replace (‘/-$/’, ”, $s)));


    return strtolower(preg_replace (‘/^-|-$/’, ”, $s ));

    Or even better, using strtolower(trim( $s, ‘-‘ )) which is much faster then an Regex.

    • 19

      Very good point – thanks for that. Using trim is much better. And thanks to everybody for the very positive comments. I’m glad you all liked the article.

  17. 20

    A very informative post. Thanks!

  18. 21

    Great post! I’ve been using using .htaccess for a couple years, but I’ve never seen a tutorial that explains the meaning of all the special characters used in .htaccess as nice as you did. It was very helpful.

  19. 22

    This is by far one of the best articles I’ve ever came across.. thank you soo much for this outstanding information…

  20. 23

    Excellent article! Thanks :)

  21. 24

    If you’re not using Apache I have found ISAPI rewrite excellent on IIS, follows similar syntax to the Apache rewrite and same functionality. Been using it for about 4 years now with no issues, except a massive txt file of rewrite rules.

  22. 25

    Amazing information!
    My only problem: shall I bookmark this, or save the entire page for future reference? I’m sure that I’ll need this information again, and I’m also sure that by then I will have forgotten most of it…

  23. 26

    If you have a website and the navigation set up: whateversitename.index.html, and a page with the collie puppies to sell to just name/link the page without having to go through all the other .htaccess stuff?

    I mean, isn’t it just as effective and seo friendly?

  24. 27

    Excellent! Will have a look to apply this as soon as possible. Thanks!

  25. 28

    Nice this is the only step that requires looking deeply into the underlying code on your website.

    nice work thank you

  26. 29

    You should be careful with rules like
    RewriteRule ^products/.*/([0-9]+)$ diy/jsp/bq/nav.jsp?action=detail&fh_secondid=$1 [NC,L]

    While it’s nice that you have URL this also allows someone to use and it will show the exact same page. You don’t want that URL to show up in your Google results. There are real-life examples of online magazine articles with “funny” URLs.

    The article suggests the checkUrl() function for redirects. You can use that same function to show 404 error on invalid URLs.

  27. 30

    David Fregoli

    November 3, 2011 2:23 am

    Would also be cool to replace the ampersand with an ‘and’ string

  28. 31

    Several examples seem to forget that the pattern in a RewriteRule is a regex. For example, ‘RewriteRule ^horses.htm$ Xu8JuefAtua.htm’ will also redirect ‘horses$htm’ and ‘horses=html’ to ‘Xu8JuefAtua.htm’.

    I personally dislike URIs along the lines of ‘’. There’s too much opaque cruft in there. IMO, it would be better to have ‘’ point at that page and ‘’ do a search for the product with the given name (also searching older names), redirecting automatically if only one match is found.

    By the way, you might want to turn off the syntax highlighting on the .htaccess examples, since it seems to be meant for a different language.

    Aside from those things, great article! This really is an excellent introduction to and overview of URL rewriting.

  29. 32

    Great post, URL rewriting can be one of the best and quickest ways to improve the usability and search friendliness of your site.

    Morgan Todd Memphis, TN

  30. 33

    Vishnu Haridas

    November 3, 2011 6:12 am

    Well explained, very clear and nice article! We can get the ideas in a single glance itself!

    I was really confused of all these things, and now I get a clear idea of URL rewriting.

    Thanks again!

  31. 34

    Priyank Sharma

    November 3, 2011 9:47 am

    Superb article. Answers all my questions!

  32. 35

    Eduardo Hernández Villa

    November 3, 2011 8:29 pm

    Hey guys when you gonna do the Joomla! section! or some book about it.

  33. 36

    Gyanomtech Studios

    November 4, 2011 3:27 am

    This is really awesome stuff. Lucidly and to the point, yet explains all the finer details to a great extent.
    Many Thanks!!

  34. 37

    Jonathan Goldford

    November 4, 2011 1:08 pm

    Excellent article. Very thorough.

  35. 38

    Alessandro Martins

    November 5, 2011 7:49 am

    There are better ways to do this, and you are assuming that everyone will use Apache and have mod_rewrite enabled, then, before offering this feature to your customer, make sure the minimum requirements.

    About the transliteration (it is more than a simple character conversion see:, the best way is to use iconv ( if available, especially if the system need to support more than one language (i18n,

    • 39

      Thanks for the comment – I didn’t know about the iconv function – that sounds like a better way of doing the transliteration. Thanks for everybody’s comments.

  36. 40

    Yet Another PHP article.

  37. 41

    Very nicely explained. Good Stuff.

  38. 42

    good timing thanks

  39. 43


    November 8, 2011 9:18 pm

    one of the most elegant post i have read for url rewriting. Just yesterday night i was searching for wordpress url rewriting and today i got yours. Thanks a lot…

  40. 44

    I tried to redirect users from a dead url
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^$ [NC]
    RewriteRule (.*)$1 [L,R=301]

    I have placed .htacess file in the vcet folder.
    It didn’t work.

    • 45

      Alessandro Martins

      November 20, 2011 3:04 pm

      Make sure you have “mod_rewrite for Apache” module enabled and AllowOverride allowing you to overwrite the Apache config files.


↑ Back to top