Skip to content

Instantly share code, notes, and snippets.

@miklcct
Created April 21, 2018 13:48
Show Gist options
  • Save miklcct/821838c94672b4e5ce22a9b923b54c98 to your computer and use it in GitHub Desktop.
Save miklcct/821838c94672b4e5ce22a9b923b54c98 to your computer and use it in GitHub Desktop.
Remove index and .php from URL
# This .htaccess will hide your index and .php in your URL, and serve the corresponding .php file
# Examples:
# /index.php -> / (index.php served if it is the DirectoryIndex)
# /abc.php -> /abc (abc.php served if abc does not exist)
# /path/to/app.php/path/info -> /path/to/app/path/info (/path/to/app.php served)
#
# Unsupported scenarios:
# file.php and file coexist (file.php can't be served)
# index.php not set as DirectoryIndex
# multiple index.* exist in a directory (don't know which to serve after it is rewritten out)
RewriteEngine On
# Remove index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index(\.[^./]*)?(\?.*)?(\s|$)
RewriteRule ^(.*)index(\.[^./]*)?$ /$1 [R=308,L]
# Remove .php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)\.php($|\?|\s|/)
RewriteRule (.*).php($|/)(.*) /$1$2$3 [R=308,L]
# Serve PHP page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule .* %{REQUEST_FILENAME}.php%{PATH_INFO} [L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment