Felipe Felix de Luca Felipe Felix de Luca - 2 months ago 6
Apache Configuration Question

What is missing or wrong with my .htaccess file?

I know nothing about .htaccess files so I would like to ask you guys some help. I will list the things I was trying to do with code and that is not working.

  • Remove .html and .php from my url (get a clean link I mean);

  • Compress files like images and .js/.css (Google speedtest says it is not happening);

  • Make the website works with and without www (standard would be without www);

Well that's it. Thank you in advance for you help!!

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
<FilesMatch "(\. (engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)? |xtmpl)|code-style\.pl|Entries.*|Repository|Root|Tag|Template)$">
Order allow,deny
Options -Indexes
RewriteEngine on
RewriteRule ^/abc/(.*)$ /new/$1 [R=301,L]
RewriteEngine on
RewriteRule ^/def/(.*)\.html$ /new2/$1.html [R=301,L]
RewriteEngine on
RewriteRule ^/ghi/(.*)\.(html|htm)$ /new3/$1.$2 [R=301,L]
RewriteEngine on
RewriteRule ^/jkl/(.*)$ http://www.mywebsite.com/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 month"
ExpiresDefault "access 1 month"


To get automatic redirection in Apache you can consider using MultiViews, which is easy to enable and backwards-compatible. For example, with this one line in your .htaccess file:

Options +MultiViews

You can now access the same file from these URLs:


After that it's a matter of changing the HTML files so they link to the URLs without the file suffix. This is a significant undertaking (but there are some tools that might help).

As for the gzip attempt, others on StackOverflow seem to recommend mod_deflate in preference to mod_gzip as it is easier to configure. See this page for instructions on using mod_deflate (just below the mod_gzip instructions).

Finally, you can use mod_rewrite to redirect to the canonical URL without the www prefix. Example for www.example.com:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^/(.*)       http://example.com/$1 [L,R=301]