![]() |
|
Useful Snippets for .htaccess - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Design (https://sinister.ly/Forum-Design) +--- Forum: Web Design (https://sinister.ly/Forum-Web-Design) +--- Thread: Useful Snippets for .htaccess (/Thread-Useful-Snippets-for-htaccess) |
Useful Snippets for .htaccess - Megamente - 07-31-2013 Hello HC!! In this tutorial i'll show you 6 wonderful snippets you can add to your .htaccess in order to do magic things! But Mega, what is a .htaccess? .htaccess is the file which control your Apache Webserver. It is located in your root directory, ready to be edited! It's very useful and allows you to do a lot of things. In this article, we are going to see 6 snippets that anyone should use ![]() WARNING: Backup! Always backup! If things go wrong, you can restore it if needed. Removing extensions from url's I dont know about you, but i hate when i see .html or .php in my url's! The following snippet will remove them for you: Code: RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, aspRemove www in url The following snippet will remove thw "www" from your url. Very useful for SEO. Code: RewriteEngine On
RewriteCond %{HTTP_HOST} !^mywebsite.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [L,R=301]Create custom error pages This snippet will redirect you to the following error page, but you need to create them first and upload them in your webserver. Code: ErrorDocument 400 /error/badreq.html
ErrorDocument 401 /error/autherror.html
ErrorDocument 403 /error/forbidden.html
ErrorDocument 404 /error/notfound.html
ErrorDocument 500 /error/serverr.htmlPrevent directory listing On your web server, when a directory do not have an index file, Apache automatically create a list of all files from the current directory. If you do not wish that anyone can see which files are on your server, just add the following code to your .htaccess file to prevent automatic directory listing. Code: Options -IndexesReduce pages weight by compressing static data Do you know that it is possible to send compressed data to the visitors, which will be decompressed by the client? This code will definitely save you (and your visitor) bandwidth and reduce your pages weight. Code: AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/htmlAutomatically add utf-8 charset to files In order to avoid encoding problems, you can force a specific encoding directly on your .htaccess file. That way, you’ll ensure that your html documents will always render correctly, even if your forget to add a <meta http-equiv="Content-Type"> directive on your html pages. Code: <FilesMatch "\.(htm|html|css|js)$">
AddDefaultCharset UTF-8
</FilesMatch>Hope you liked it! Credits: css-tricks.com, askapache.com, catswhocode.com and myself
RE: Useful Snippets for .htaccess - bluedog.tar.gz - 07-31-2013 Nice and usefull post. I'll be sure to bookmark this and share it with people that question this. I get these kind of questions alot
RE: Useful Snippets for .htaccess - The Alchemist - 08-01-2013 Awesome tutorial. Just one more addition I'd like to ask you to add : Code: Options -IndexesRE: Useful Snippets for .htaccess - Megamente - 08-01-2013 Quote:Nice and usefull post. Thanks! :Smile: Quote:Awesome tutorial. Thanks man! Just edited the post with your suggestion.
RE: Useful Snippets for .htaccess - Lily_mybb_import12771 - 08-02-2013 very userful for SEO . yse or no , RE: Useful Snippets for .htaccess - soh_cah_toa - 08-27-2013 Nicely done. I have another useful tip to chime in. You can restrict access to a particular folder according to IP address by adding: Code: order deny,allow
allow from 10.0.1.2
deny from allNeedless to say, you must have a static IP address in order for this to be effective. As an example, Wordpress users might do this to prevent anybody but them from accessing the admin dashboard. Similarly, you can also ban arbitrary IP's by using: Code: <Limit GET POST>
order allow,deny
deny from 10.0.20.3
deny from 10.0.20.4
deny from 10.0.20.5
# etc...
allow from all
</Limit>RE: Useful Snippets for .htaccess - Sebkvernland - 10-25-2013 I found this very usefull! Thank you
RE: Useful Snippets for .htaccess - Sebkvernland - 10-25-2013 I found this very usefull! Thank you
RE: Useful Snippets for .htaccess - Sebkvernland - 10-25-2013 I found this very usefull! Thank you
|