0

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.*
</ifModule>
<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
</FilesMatch>
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"
</IfModule>
  • `the things I was expecting to happen, are not happening` So what exactly is that? – Panama Jack Aug 02 '16 at 01:16
  • Hello @PanamaJack, I was trying to remove the extension (.html) from my pages. I collected some information about .htaccess files and tried to write my own, but my pages still have the extensions on it. Also, when I check Google speedtest it says my website doen't compress the images and files. I also wanted to make it with that .htaccess. – Felipe Felix de Luca Aug 02 '16 at 01:19
  • Explain what you mean by removing extension from your page. Are you expecting the html extensions to be removed from your code? Or do you want to use pretty URL's? please explain better and what you expect to see in the address bar. – Panama Jack Aug 02 '16 at 01:24
  • 1
    Some tips to start: you can only use one RewriteEngine directive (i.e. don't keep turning it on multiple times); your first rewrite is a no-op (it does nothing but terminate the rewrite chain with the L, which means "last"); you should separate the removal of extensions from the attempt to gzip; and you have multiple rules with different prefixes - is that just for testing? – Owen Aug 02 '16 at 01:25
  • @PanamaJack I rewrited my question. I think it got easier to understand now. – Felipe Felix de Luca Aug 02 '16 at 01:30
  • @Owen hello! Thank you for your tips. No actually I want to use it for a project. The thing is it is my first time using .htaccess and I know nothing on how to write it. – Felipe Felix de Luca Aug 02 '16 at 01:32
  • 1
    @FelipeFelixdeLuca: That's great! Each task you are trying to achieve in .htaccess can be implemented independently. Please try one at a time - I suggest you start with the URL rewriting (i.e. remove the gzip attempts, etc.) and when that's working move on to the next one. – Owen Aug 02 '16 at 01:38

2 Answers2

1

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:

/def/search.html
/def/search

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]
Community
  • 1
  • 1
Owen
  • 1,527
  • 11
  • 14
1

Why not use a file like here:

GitHub - Apache Server Configs

This .htaccess already has configurations for compression etc.

I think that you will be able to add to the file everything that need for you for example you can add type of flashplayer's files.

A. Petrov
  • 902
  • 1
  • 6
  • 6