Welcome to a quick tutorial on how to hide the PHP file extension in the URL. Don’t want to show an ugly PHP file extension or want to hide it away for security purposes?
An easy way to hide the PHP extension while using the Apache web server is to create a .htaccess
file with the following lines:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This will now “map” http://site.com/PAGE
to http://site.com/PAGE.php
.
This covers the quick basics, but let us walk through a couple of examples to play with the URL rewrite – Read on!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
EXTENSION HIDING
All right, let us now get into the various examples of hiding the PHP file extension.
METHOD 1) OFFICIAL SOLUTION – OBFUSCATED FILE TYPE
# (A) CONFUSE PEOPLE - PY OR ASP FILES ACTUALLY CONTAIN PHP CODE
AddType application/x-httpd-php .py .asp
# (B) OR USE AN UNKNOWN FILE TYPE
AddType application/x-httpd-php .foo .bar
Believe it or not, this is the actual official solution from the PHP website. What we do is rename .php
into another extension such as .py
, .asp
, or .foo
, but process them as PHP scripts in the .htaccess
file. While I personally don’t agree with it, it does somewhat serve to confuse people.
P.S. This is a bad solution, what if the server has to support more than one language? This will definitely clash with Python, ASP, and cause serious confusion among developers.
METHOD 2) SIMPLE URL REWRITE
# (A) REWRITE ON
RewriteEngine on
# (B) DO NOT OVERRIDE EXISTING FOLDERS
RewriteCond %{REQUEST_FILENAME} !-d
# (C) REWRITE PHP FILES ONLY
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
As in the introduction above, what this means in English:
RewriteEngine on
Turn the URL rewrite engine on.RewriteCond %{REQUEST_FILENAME} !-d
Do not override existing folders.RewriteCond %{REQUEST_FILENAME}.php -f
Rewrite only if it is a.php
file.RewriteRule ^([^\.]+)$ $1.php [NC,L]
Attempt to “map”http://SITE.com/PAGE
tohttp://SITE.com/PAGE.php
.
Yep, as simple as that. Four lines, problem solved.
METHOD 3) SIMPLE PRETTY URL WITH PHP
3A) HTACCESS – POINT EVERYTHING TO INDEX
# (A) REWRITE ON
RewriteEngine On
# (B) DO NOT OVERRIDE HTTP AUTH
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# (C) MAKE SURE THE REWRITEBASE IS CORRECT
# I.E. IF HTTP://SITE.COM/FOLDER/, BASE SHOULD BE /FOLDER/
RewriteBase /
# (D) DO NOT OVERRIDE EXISTING FILES FOLDERS
RewriteRule ^3b-index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# (E) MAKE SURE PATH IS CORRECT
# IF HTTP://SITE.COM/FOLDER/, SHOULD BE /FOLDER/3B-INDEX.PHP
RewriteRule . /3b-index.php [L]
Combining .htaccess
with PHP, we can create a good page template system – Here is the .htaccess
file “borrowed” from WordPress. Not going to explain line by line. But in plain English, this means “point everything to 3b-index.php
but leave the existing files and folders alone”.
3B) INDEX – PAGE LOADER & HANDLER
<?php
// (A) SETTINGS
define("URL_PATH", "/"); // CHANGE THIS IF NOT ROOT!
define("PATH_PAGES", __DIR__ . DIRECTORY_SEPARATOR . "pages" . DIRECTORY_SEPARATOR);
// (B) STRIP PATH DOWN TO AN ARRAY
// HTTP://SITE.COM/HELLO/WORLD/ > $_PATH = ["HELLO", "WORLD"]
$_PATH = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$_PATH = substr($_PATH, strlen(URL_PATH));
$_PATH = rtrim($_PATH, '/');
$_PATH = explode("/", $_PATH);
// (C) FILE NAME YOGA
// $_PATH = ["HELLO", "WORLD"] > $_FILE = "HELLO-WORLD.PHP";
// $_PATH = [] > $_FILE = "HOME.PHP";
if (count($_PATH) > 1) { $_FILE = implode("-", $_PATH) . ".php"; }
else { $_FILE = ($_PATH[0]=="" ? "home" : $_PATH[0]) . ".php"; }
// (D) LOAD PAGE - CREATE YOUR OWN HTML TEMPLATE IF YOU WANT
if (file_exists(PATH_PAGES . $_FILE)) {
// require "TOP-HALF.PHP";
require PATH_PAGES . $_FILE;
// require "BOTTOM-HALF.PHP";
}
// (E) NOT FOUND - SERVE YOUR OWN CUSTOM 404 PAGE IF YOU WANT
else {
http_response_code(404);
echo "OOPS. FILE NOT FOUND";
}
In the “index page handler”, we simply break the path portion of the URL down and load the respective page. For example, http://site.com/test/
will load pages/test.php
. Yep, feel free to create your own HTML template system here, or even load contents from the database.
3C) DUMMY PAGE
<!DOCTYPE html>
<html>
<head>
<title>TEST!</title>
</head>
<body>
<p>This is a test page!</p>
</body>
</html>
EXTRAS
That’s all for this tutorial, and here is a small section on some extras and links that may be useful to you.
HOW ABOUT IIS & NGINX?
If you using IIS or NGNIX – Don’t worry. Both of them also offer URL rewrite, and the idea behind this guide will still kind of work. But the syntax for both web servers is different, and you will need to do some homework.
LINKS & REFERENCES
- URL Rewrite – Apache
- Allow Override Directive – Apache
THE END
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
Thanks! However, if you do not want .htaccess files, you can do this in httpd.conf directly for a specific directory.
<Directory /FOLDER>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</Directory>
Hi there,
I have followed your instructions and the template worked for the most part but I ran into this one issue. After I log the users in, I redirect them to the home page using header(‘Location: https//www.mysite.com/member/home’). It works if the trailing slash after home isn’t there; however, if I redirect using header(‘Location: https//www.mysite.com/member/home/’) (with trailing slash) and then try to call another page say stock from my home page, the stock is appended to the url and it obviously does not work i.e. https://www. mysite.com/member/home/stock/
Is there any way that I can make it work for both with trailing slash and without trailing slash. Clicking on other menu options would simply keep appending i.e. https://www. mysite.com/member/home/stock/logout/home/ and it’s no going anywhere.
I did have an issue with my css and then I used absolute path based on your answer to another use and it had it fixed.
I thank you in advance.
Force append trailing slash to the url automatically.
Hi there,
I implemented your pretty URL option and it worked; however, I ran into an issue. I am calling .css files in my top.php or header.php file and if I call the php files ending with a trailing /, the PATH adds the name of the file to the path. For example, if I call
www.mysite/contact/
, the .css file in my header (which I call “css/screen.css”) is prepended withwww. mysite .com/contact/css/screen.css
as opposed towww.mysite.com/css/screen.css
and therefore doesn’t render the css because it can’t find it.if I call the without a trailing slash i.e.
www.mysite/contact
then it works fine because with trailing / absent, it prepends the css file withwww.mysite.com/css/screen.css
any idea what needs to be done.
thanks
Sam
Use absolute URL, not relative. In PHP –
define("URL_CSS", "https://site.com/css/")
, then in HTML<link rel="stylesheet" href="<?=URL_CSS?>STYLES.css">
In order to redirect everything to index.php, you could use simply (.htaccess):
FallbackResource /index.php