Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/c4eec742337a8c0547188ae561e8f87f to your computer and use it in GitHub Desktop.
Save code-boxx/c4eec742337a8c0547188ae561e8f87f to your computer and use it in GitHub Desktop.
Hide PHP Extension In URL using HTACCESS

HIDE PHP FILE EXTENSION IN URL

https://code-boxx.com/hide-php-extension-url-htaccess/

NOTES

  1. GIST does not allow folders. Create a pages/ folder and move test.php inside.
  2. Please enable the URL rewrite module in httpd.confLoadModule rewrite_module modules/mod_rewrite.so
  3. Also in httpd.conf, make sure that AllowOverride is properly set for your HTTP folder (the easiest is to set AllowOverride All).

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# (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
# (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]
# (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]
<?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";
}
<!DOCTYPE html>
<html>
<head>
<title>TEST!</title>
</head>
<body>
<p>This is a test page!</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment