Quantcast
Viewing all articles
Browse latest Browse all 11

Remove .php extensions with .htaccess

You can clean up your URL a little by automatically removing the .php extensions and index files using mod_rewrite. This requires your web server has Apache running the mod_rewrite module. Create an .htaccess file in your web root directory with this content:

# Author: Lil Josh (liljosh.com)
# Date: Sept 2014
# Description: Clean up the URL by removing the .php extension and index file

RewriteEngine On
RewriteBase /

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php

# Remove index file
RewriteRule ^(.*)index\.(html|htm|shtml|php)$ http://%{HTTP_HOST}/$1 [R=301,L]

Viewing all articles
Browse latest Browse all 11

Trending Articles