logo
Page Affairs

Editing and Web Design

A Tip on Neat URLs

You may have seen neat URLs (that is, web addresses) something like this: somesite.com/news. A URL like that is usually created “dynamically”, by some kind of content management system running on PHP or some other server side language.

Another way to create a URL like that is via a “URL rewrite”, which can be a somewhat complex process involving things like .htaccess files and some complex code.

Trick On Static Sites

But if you just have a static, hand-coded site (and yes, I think they are still quite valid!) there is a simple alternative that is very elegant, I think.

Let’s say you have a Contact page on your site, the address of which is currently mysite.com/contact.php. Hm, not so pretty, if you ask me. Rather than your Contact page appearing in the address bar as contact.php or contact.html, it looks nicer to have a clean URL like mysite.com/contact/.

The trick to this is to place your contact page inside a folder called /contact/ and then call the file index.php or index.html. When you point your browser to a folder like /contact/, the server looks for an “index” file in that folder and serves it up by default (but without actually displaying the file name).

And that’s it!

You can do that with any file. Say you wanted to display a Thank You page once site visitors have submitted your contact form. Instead of something like mysite.com/contact/thankyou.html, you could create a /thankyou/ folder inside your /contact/ folder, place your thankyou.html page inside that /thankyou/ folder and rename it to index.html. That way, the address of your Thank You page will now appear as mysite.com/contact/thankyou/.

Legacy Comments

Jake Thomas — May 27, 2015

Why don’t you just use mod_rewrite, like so :

Make a .htaccess file in your root file then inside of the file enter:

RewriteEngine on

RewriteRule ^contact/thankyou/?$ thankyou.html

So much easier.

Ralph Mason — May 27, 2015

Yep, fair point, Jake! I’m not totally convinced it’s easier, as rewrite rules can get rather tricky, but that’s definitely a viable alternative.

© Page Affairs, 2008–2024