Skip to content

24 ways to impress your friends

URL Rewriting for the Fearful

47 Comments

Comments are ordered by helpfulness, as indicated by you. Help us pick out the gems and discourage asshattery by voting on notable comments.

Got something to add? You can leave a comment below.

Jeremy Keith

This is great stuff! I’m terrible at regular expressions—my brain just doesn’t seem to want to remember any of it—but this article contains the clearest description of regular expressions I’ve come across.

I thought I’d share a useful little rewrite rule that I use for cache-busting JavaScript and CSS files. You know the story: you make a change in your JavaScript or CSS and you want to let the browser know that it should grab the new version instead of using what it’s got in its cache.

Now, I could potentially just use a query string when I point to my JS and CSS files ( e.g. /js/myscript.js?20131201 ) …but that can cause issues with proxy servers.

Instead what I what I do is point to files like this:

/js/myscript.20131201.js

Then I need to tell the server to look for the actual file in /js/myscript.js

Here’s the rewrite rule I’m using:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.).(d).(js|css)$ $1.$3 [L]

It’s basically telling the server that, if the JS or CSS file doesn’t actually exist and it matches the pattern of having two dots before the file extension (with only numbers after the first dot), to look at the bit before the first dot, and look at the bit after the second dot, but to ignore the bit in between (the numbers).

The server serves up the right file, but browsers fetch the new version because, as far as they’re concerned, this looks like a brand new file that they haven’t got in their cache.

That was a terrible explanation, wasn’t it? I now have even more appreciation for how clearly and concise this article is.

Matt Steel

This is, by far, the most human readable explanation of .htaccess redirect rules I’ve come across.

I (very poorly) wrote some redirects just this past week and walked away considering it some form of dark art. The mystery is now (thankfully) gone. I’ll bookmark this article and come back to it down the line for sure.

Thanks for taking the time to write this out Drew.

Mathias Bynens

In these two examples from the article, the dot should be escaped:

bc. RewriteRule ^article/used/to/be/here.php$
RewriteRule ^about/([a-z0-9-]+).jsp/$ /about/$1/ [R=301,L]

Nicolas Chevallier

Happy to read 24ways again after a year!

The article is very educational, but there is one thing to clarify in my opinion : put the rewrite rules in the .htaccess file is an easy way in development environment, but in production it’s better to put these rules in apache configuration file and disable the use of .htaccess files, otherwise the .htaccess file will be read whenever a user accesses a webpage. (and even several times if images, css or js also have rules).

Radu

The QSA flag isn’t necessary in your example, as the query string is preserved by default. It is only needed when the replacement URI also contains a query string, and you want to append the original string to it.

Tom Turton

I’ve read many URL rewriting tutorials, but none I could fully get my head around. This one was an exception: I was reading it on my phone at 6.40am with a far-too-awake toddler tugging on my jumper and think I get it now!

Actually, this article is worth a mention for the regex intro alone.

A great start to 24 Ways 2013!

Jim Camomile

What Matt and Bob said (and many many others are thinking but did not take time to comment)… “This is, by far, the most human readable explanation of .htaccess redirect rules I’ve come across.”

I am truly grateful for you taking the time to making sense of this for me.

Stomme poes

@ Berend Bierenbroodspot

If you are on shared hosting and have no access to root, .htaccess file would be in the root dir of whatever you’re running a web page with: for example, if you are using WordPress, there’s a root WordPress directory with all the .php files and stuff. If there’s not already a file there called .htaccess, you can simply make one. The server actually running your WP stuff will probably check for it, every request (this is why .htaccess is not preferred if you have root access: https://httpd.apache.org/docs/2.2/howto/htaccess.html and is a small, but regular, performance penalty).

If you do have root access, let’s say you’re on a Debian-based machine for example… you may have files with names like /sites-enabled/default or sites-available/default under a directory called apache2 (we’re assuming you’re using Apache). At the bottom of one of those files, you can add rewrite rules, starting with “RewriteEngine on”.

If you’ve only just installed your machine or apache you may have to explicitly enable the rewrite.so stuff on, since it may be off by default. You can google/DuckDuckGo around for how to do all that, but this article is assuming the .htaccess file.

Arun

hey i am use this code to change my url – RewriteRule ^create_account/$ registration/index.php

but in registration folder have js file and i m access this file using
this path /registration/account.js but i want change this path.
how can i do this…?

Gunnar Bittersmann

Unfortunetly, the article misses “one of the most important concepts about mod_rewrite – namely, when to avoid using it.” Apache documentation

In many cases, RewriteRule should not be used. Often the same goal can be achieved with Redirect or RedirectMatch . For removing file extensions, MultiViews would be the way to go.

This artivle propagates the unreflected usage of RewriteRule and spreads half-knowledge. I consider this harmful.

Jelmer Borst

Regarding Nicolas Chevallier’s comment:
I agree with you on that bit, however not everybody has access to their Apache config. Therefore they/we are bound to use .htaccess

What I hear Drew saying in the Perch podcasts, are that a lot of people are on shared hosting. So very likely that they won’t have access, unfortunately.

pceasies

You can also combine URL rewriting with some optimization tools to keep your code clean while serving files that aren’t filled with comments and unnecessary white space. I made a script that uses YUI, LESS, and a couple other tools on my CSS and JS files and copied them to a “compressed” directory. All this gets uploaded and the rewrite rules change anything that points to “assets” to “assets/compressed.” This way I can keep clean code on my side, and serve content that doesn’t waste bandwidth on the other side. This can also be combined with version control and FTP/upload so you have a working directory and one script that prepares your website and updates/uploads it.

Len

Drew, just want to echo some of the comments above not just the most readable explanation of .htaccess redirect rules comprehend able regex. More please!

Muhammad Danish

I usually don’t read articles that talk about rewriting, buty ou explain this subject so <a haref=“http://anytimegame.blogspot.com”>brilliantly</a>. I’ve had issues understanding how to remove file extensions for a while now and just reading this article enabled me to not only get it working but also understand the whole process.

Ralph

The article starts well, but gets fuzzy towards the end.The “Removing file extensions” section left me totally confused. It looks like the code is adding a .php extension rather than removing it. Obviously I’m not understanding, but that was the point of the article. :-)

Brian LePore

First, I am happy that 24ways is back.

Second, we always set up 301 redirects for old pages from a site to their appropriate new pages when we re-launch a site. We just picked up a client and the old site apparently got too fancy with their AJAX/HTML5 stuff and used a hashbang notation (i.e. example.com/#!menu-chicken ). Does anyone know a best practice way of handling that?

I found that the site DOES support AJAX Crawling ( https://developers.google.com/webmasters/ajax-crawling/docs/getting-started ) which we can redirect, but has anyone had to deal with redirecting sites based on this? Are we gonna need to set up redirects on the server for the AJAX Crawling, and then write a client side redirect on the home page?

Eric

I’ve had this saved in my bookmarks for a while now and finally got around to reading it when I got into a jam. Thank you, thank you, thank you.

Julia Evansq

Thank you, thank you, thank you.
I hate url rewrites, particularly redirects – I don’t need to do them very often but when I do, I have to re-learn or get someone else to show me how ‘again’. I shall keep this article by my side at all times for when the dreaded deed occurs again – very helpful thanks.

Nicky Speakman

Hi, First off awesome article. Much easier to read and understand than others that I have seen. I have used:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

Which works. But the one issue I have is if my url is: domain.com/admin/ the ending / after the page name makes all of the directories show.

I would either like it to redirect removing the / or allow the /

Thanks

neemzy

A clear and very enjoyable read about URL rewriting, thank you !

Sometimes the possibilities offered by Apache just aren’t enough though, depending on what level of flexibility you need to achieve. In such cases, you may be better off simply redirecting every incoming request to a single index.php file (assuming you’re using PHP) and handle it there depending on the requested URL.

Molly

@Rudie — I think you mean a link to http://www.w3.org/Provider/Style/URI ;-)

But seriously, thanks for that link. I hadn’t thought about dropping the file extension, as that page suggests, before now. I am interested to hear opinions on whether that particular suggestion still makes sense 15 years on.

Mark

Great article.

The thing that has always troubled me over the years is why mod rewrite is so difficult to use unless you are a whizz at regular expressions. Isn’t it about time that this module was simplified so anybody could use it?

Jay Tillery

It’s so difficult to explain regex, .htaccess and url rewriting, however, I must admit that your talent of explaining such complexity so that the reader could understand without becoming frustrated is a gift. Awesome job!

Theo

In the most cases i only have the htaccess file to work with so thank you very much for this article!

Sorry to say it, but i think “Demystifying the redirect rules” would be a proper title.

Crystal Adkins

Finally, someone put redirects/rewrites in terms I could actually understand. I’ve had a helluva time trying to figure it all out. You just don’t know how much I appreciate this. Thank you, thank you, thank you!

Matt Willmott

Drew, you explain this subject so brilliantly. I’ve had issues understanding how to remove file extensions for a while now and just reading this article enabled me to not only get it working but also understand the whole process. I never thought that would happen!

Prisca

Thanks so much for this article ~ so clear and concise. I’m always worried when editing the .htaccess ~ now I have a great reference to make me feel less nervous :)

Thank you, Drew.

MIchael Hastrich

Couldn’t agree more with Matt. I looked at rewrites before and never dared to touch them, worrying I would break not only the site I was working on but, for all I knew, the entire Internet :)

Thanks for this, a great start to 24ways 2013!

Impress us

Be friendly / use Textile