“Learn .htaccess rules for omitting file extensions, enhancing website aesthetics, and improving SEO. Example code and explanations included.”
Example Code:
# Remove .php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# Remove .html and .htm extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f [OR]
RewriteCond %{REQUEST_FILENAME}\.htm -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]Explanation:
- Enable Rewrite Engine:
- RewriteEngine On: Activates the Apache rewrite engine.
- Remove .php Extension:
- RewriteCond %{REQUEST_FILENAME} !-d: Checks if the requested file is not a directory.
- RewriteCond %{REQUEST_FILENAME}.php -f: Checks if the requested file with .php extension exists.
- RewriteRule ^([^\.]+)$ $1.php [NC,L]: Redirects URLs without extension to their .php counterparts.
- Remove .html and .htm Extensions:
- RewriteCond %{REQUEST_FILENAME} !-d: Ensures the requested file is not a directory.
- RewriteCond %{REQUEST_FILENAME}\.html -f [OR]: Checks if the file with .html extension exists.
- RewriteCond %{REQUEST_FILENAME}\.htm -f: Checks if the file with .htm extension exists.
- RewriteRule ^([^\.]+)$ $1.html [NC,L]: Redirects URLs without extension to their .html counterparts.
Enhance your website's aesthetics and SEO by implementing these .htaccess rules, providing cleaner and user-friendly URLs.
