Friendly URLs for static .php files using .htaccess

Setting up an .htaccess file has been a nigtmare. Let’s have a closer look into it and its rules/conditions.

In case you’re new to the force, .htaccess files are, according to the

.htaccess is a configuration file for use on web servers running the Apache Web Server software.

By manipulating it, you can do loads of things. Redirects, block access to folders and files, set caching time limits for site files (Google recommends doing it), etc.

I’ll go through a few tricks to generate Friendly URLs for static .php files on your website.

Friendly URLs for

I am currently building way360group’s website, by the time this post goes live, the site will have been published so have a look at it here:

By friendly URLs I mean something like displaying instead of

No one wants users to feel confused when navigating through the site, and, showing file extensions may scare them — and you also may get punished by Search Engines ranking systems, which tend to rank you down in case your URL’s are not ~eligible~.

I will it keep it really simple here, and this case only applies if you’ve got static files on your server — it won’t create dynamic URL’s.

Let’s get started:

First things first, you have to enable the Rewrite Engine:

RewriteEngine On

Now that your server is allowed to rewrite URL’s, you have to set Rules.

RewriteRule ^contact/?$ contact.php

This rules says that in case the URL “” is being accessed, it will be rewritten to “”.

Simple, hey? Yes, but, you have to do it for all files and make sure your menu has it all correct.

In my case, my .htaccess is looking like this:

RewriteEngine On

RewriteRule ^contact/?$ contact.php
RewriteRule ^what-is-corporate-social-responsibility/?$ what-is-corporate-social-responsibility.php
RewriteRule ^about-us/?$ about-us.php
RewriteRule ^latest-projects/?$ latest-projects.php
RewriteRule ^join-our-newsletter/?$ join-our-newsletter.php
RewriteRule ^error/?$ errorpage.php

Once I’ve got only 6 pages, wasn’t a big deal for me to write all rules.

That’s it! Hope your URLs are friendly as mine are by now.

Additional .htaccess rules

On top of the awesome and super-friendly URLs, you’re also able to manipulate where to redirect users in case they encounter an error:

//Custom 400 errors
ErrorDocument 400 /404.php

//Custom 401 errors
ErrorDocument 401 /404.php

//Custom 403 errors
ErrorDocument 403 /404.php

//Custom 404 errors
ErrorDocument 404 /404.php

//Custom 500 errors
ErrorDocument 500 /404.php

And now that Google’s crazy about security on web, “forcing” your users to use your https:// comes handy. You can achieve that by setting up this rule:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Some extra references

Added Bytes — URL Rewriting for Beginners

.htaccess Browser Based Generator — this is GOLD!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store