With over a decade of web development experience, I specialize in Drupal (7, 8, 9, 10), CodeIgniter, Laravel, and WordPress. I offer extensive expertise in both module and theme development, providing customized solutions for complex projects. Whether you need to enhance an existing platform, create new features, or seek expert guidance, I'm here to assist. My dedication to delivering high-quality, efficient, and scalable solutions is unmatched. Feel free to contact me to explore how I can contribute to your project's success. Let's turn your ideas into reality!

“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:

  1. Enable Rewrite Engine:
    • RewriteEngine On: Activates the Apache rewrite engine.
  2. 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.
  3. 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.

Posted by Sujan Shrestha
Categorized:
PREVIOUS POST
banner