Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active October 5, 2023 12:48
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/b36dc6428e70d26ea75232773da82d99 to your computer and use it in GitHub Desktop.
Save code-boxx/b36dc6428e70d26ea75232773da82d99 to your computer and use it in GitHub Desktop.
PHP HTACCESS Pretty URL
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

PHP HTACCESS PRETTY URL

https://code-boxx.com/pretty-url-php-htaccess/

NOTES

  1. GIST does not allow folders. Manually create a pages folder, move 404.html, about.html, index.html inside.
  2. Recommended - Create pages/.htaccess with one line Deny from all. This will disallow direct access, I.E. http://site.com/pages/about.html will throw 403 forbidden.
  3. Make sure that mod_rewrite is enabled in Apache.
  4. Change the path in .htaccess if not deployed in the base path. For example, http://site.com/DEMO/ - RewriteBase /DEMO/ and RewriteRule /DEMO/ index.php [L]
  5. Also change the base URL in index.php if not deployed in the base path - define("URL_BASE", "/DEMO/")

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.

<html>
<head>
<title>NOT FOUND</title>
</head>
<body>
<h1>PAGE NOT FOUND</h1>
<p>Opps. Page might have been abducted by aliens.</p>
</body>
</html>
<html>
<head>
<title>About Us</title>
</head>
<body>
<h1>ABOUT PAGE</h1>
<p>The dummy about page.</p>
</body>
</html>
<html>
<head>
<title>Pretty URL Demo</title>
</head>
<body>
<h1>HOME PAGE</h1>
<p>The dummy home page.</p>
</body>
</html>
<?php
// (A) SETTINGS
define("URL_BASE", "/");
define("PATH_PAGES", __DIR__ . DIRECTORY_SEPARATOR . "pages" . DIRECTORY_SEPARATOR);
// (B) PARSE URL PATH INTO AN ARRAY
// e.g. http://site.com > [""]
// e.g. http://site.com/foo > ["foo"]
// e.g. http://site.com/foo/bar > ["foo", "bar"]
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
if (substr($path, 0, strlen(URL_BASE)) == URL_BASE) {
$path = substr($path, strlen(URL_BASE));
}
$path = explode("/", rtrim($path, "/\\"));
// (C) LOAD REQUESTED PAGE ACCORDINGLY
// (C1) URL PATH TO PHYSICAL FILE
// e.g. http://site.com > index.html
// e.g. http://site.com/foo > foo.html
// e.g. http://site.com/foo/bar > foo-bar.html
if (count($path)==1) {
$file = $path[0]=="" ? "index.html" : $path[0] . ".html";
} else {
$file = implode("-", $path) . ".html";
}
// (C2) LOAD PHYSICAL FILE
if (file_exists(PATH_PAGES . $file)) { require PATH_PAGES . $file; }
else {
http_response_code(404);
require PATH_PAGES . "404.html";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment