{"id":812,"date":"2012-03-02T16:05:21","date_gmt":"2012-03-02T15:05:21","guid":{"rendered":"http:\/\/www.venco.com.pl\/~cozy\/blog\/?p=812"},"modified":"2012-03-02T16:05:21","modified_gmt":"2012-03-02T15:05:21","slug":"htpaccess","status":"publish","type":"post","link":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/2012\/03\/02\/htpaccess\/","title":{"rendered":"htpaccess"},"content":{"rendered":"<p><a href=\"http:\/\/www.venco.com.pl\/~cozy\/blog\/wp-content\/uploads\/2012\/03\/htaccess.pdf\" target=\"_self\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"http:\/\/www.venco.com.pl\/~cozy\/blog\/wp-includes\/images\/crystal\/document.png\" alt=\"\" width=\"46\" height=\"60\" \/><\/a><\/p>\n<section class=\"article_body\">\n<h2>\nCreating and Uploading an .htaccess File<\/h2>\n<p>Creating an .htaccess file is very easy.<\/p>\n<p>Simply open Notepad or a similar text-based program, switch off word-wrap, add the code and save the file in the usual way.<\/p>\n<p>For example, you could call it:<\/p>\n<pre>htaccess.txt<\/pre>\n<p>Upload the file to the relevant directory on your web server and then rename it like so:<\/p>\n<pre>.htaccess<\/pre>\n<p>Remember, the .htaccess file should be using 644 permissions and uploaded in ASCII mode. If your .htaccess file does not work, then you should contact your system administrator or web hosting company and ensure they have enabled \u2018.htaccess\u2019 within your account, as some web hosting companies do not allow its use without prior permission. Unfortunately, .htaccess will not work on Windows-based servers.<\/p>\n<h2>Using .htaccess<\/h2>\n<p>It is important to remember that an .htaccess file will affect the directory it is placed in and all resulting sub-directories. Therefore, if you add your \u2018.htaccess\u2019 file to the \u2018web site root\u2019 then it will affect all subsequent folders like so:<\/p>\n<pre>http:\/\/www.yourdomain.com\/\n| -- directory1\n| -- directory2\n| -- directory3\n|    | -- directory3\/childdirectory1\n|    | -- directory3\/childdirectory2\n| -- .htaccess\n| -- index.html<\/pre>\n<p>However, if you place the \u2018.htaccess\u2019 file in http:\/\/www.yourdomain.com\/directory1 then the features of the \u2018.htaccess\u2019 will be restricted to that folder and all child folders only. For example:<\/p>\n<pre>http:\/\/www.yourdomain.com\/\n| -- directory1\n|    | -- directory1\/childdirectory1\n|    | -- directory1\/childdirectory2\n|    | -- directory1\/childdirectory3\n|    |    | -- directory1\/childdirectory3\/newdirectory1\n|    |    | -- directory1\/childdirectory3\/newdirectory2\n|    | -- .htaccess\n|    | -- index.html<\/pre>\n<p>After editing your .htaccess file on multiple occassions it may look a little complicated so I would recommend implementing comments. To do this, simply place the hash symbol at the beginning of every line like so:<\/p>\n<pre># comment here\n# another comment here<\/pre>\n<h2>Useful Snippets<\/h2>\n<p>And to get you started, it\u2019s snippet time \u2026<br \/>\n(although one or two of them are strictly directives for Apache)<\/p>\n<h3>Directory Index<\/h3>\n<p>You can change a default index file of directory with:<\/p>\n<pre>DirectoryIndex welcome.html welcome.php<\/pre>\n<h3>Custom Error Pages<\/h3>\n<p>You can redirect your users to an error page with:<\/p>\n<pre>ErrorDocument 404 error.html<\/pre>\n<p>And you can extend this like so:<\/p>\n<pre>ErrorDocument 400 \/400.html\nErrorDocument 401 \/401.html\nErrorDocument 403 \/403.html\nErrorDocument 404 \/404.html\nErrorDocument 500 \/500.html\nErrorDocument 502 \/502.html\nErrorDocument 504 \/504.html<\/pre>\n<p>But remember to create your error pages!<\/p>\n<h3>Remove the Need for www in Your URL<\/h3>\n<p>Keep your site consistent by removing the need for \u2018www\u2019 by using:<\/p>\n<pre>RewriteEngine On\nRewriteBase \/\nRewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]\nRewriteRule ^(.*)$ http:\/\/yourdomain.com\/$1 [L,R=301]<\/pre>\n<h3>Set the Time Zone for Your Server<\/h3>\n<pre>SetEnv TZ Europe\/London<\/pre>\n<h3>Control Access to Files<\/h3>\n<p>Most people will remember that .htaccess is most often used to restrict or deny access to individual files and folders and you can do this like so:<\/p>\n<pre>deny from all<\/pre>\n<p>However, if you would like to be more specific and ban a specific IP address then you could use:<\/p>\n<pre>order allow,deny\ndeny from XXX.XXX.XXX.XXX\nallow from all<\/pre>\n<p>or alternatively for several IP addresses, you could use:<\/p>\n<pre>allow from all\ndeny from 145.186.14.122\ndeny from 124.15<\/pre>\n<h3>301 Permanent Redirects<\/h3>\n<p>Worried about those old links? Then try:<\/p>\n<pre>Redirect 301 \/olddirectory\/file.html http:\/\/www.domainname.com\/newdirectory\/file.html<\/pre>\n<h3>Set the Email Address for the Server Administrator<\/h3>\n<p>By using the following code you can specify the default email address for the server administrator:<\/p>\n<pre>ServerSignature EMail\nSetEnv SERVER_ADMIN webmaster@domain.com<\/pre>\n<h3>Detecting Tablets and Redirecting<\/h3>\n<p>If you would like to redirect tablet-based users to a particular web page or directory, try:<\/p>\n<pre>RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$\nRewriteRule ^(.*)$ http:\/\/yourdomain.com\/folderfortablets [R=301]\nRewriteCond %{HTTP_USER_AGENT} ^.*Android.*$\nRewriteRule ^(.*)$ http:\/\/yourdomain.com\/folderfortablets [R=301]<\/pre>\n<h3>Link Protection<\/h3>\n<p>Concerned about hotlinking or simply want to reduce your bandwidth usage? Try experimenting with:<\/p>\n<pre>Options +FollowSymlinks\nRewriteEngine On\nRewriteCond %{HTTP_REFERER} !^$\nRewriteCond %{HTTP_REFERER} !^http:\/\/(www.)?domainname.com\/ [nc]\nRewriteRule .*.(gif|jpg|png)$ http:\/\/domainname.com\/img\/hotlink_f_o.png [nc]<\/pre>\n<h3>Force \u201cFile Save As\u201d<\/h3>\n<p>If you would like force users to download files rather than view them in the browser you could use:<\/p>\n<pre>AddType application\/octet-stream .csv\nAddType application\/octet-stream .xls\nAddType application\/octet-stream .doc\nAddType application\/octet-stream .avi\nAddType application\/octet-stream .mpg\nAddType application\/octet-stream .mov\nAddType application\/octet-stream .pdf<\/pre>\n<p>or you simplify this as:<\/p>\n<pre>AddType application\/octet-stream .avi .mpg .mov .pdf .xls .mp4<\/pre>\n<h3>Rewrite URLs<\/h3>\n<p>If you would like to make your URLs a little easier to read (ie changing content.php?id=92 to content-92.html) you could implement the following \u2018rewrite\u2019 rules:<\/p>\n<pre>RewriteEngine on\nRewriteRule ^content-([0-9]+).html$ content.php?id=$1<\/pre>\n<h3>Redirect Browser to https<\/h3>\n<p>This is always useful for those who have just installed an SSL certificate:<\/p>\n<pre>RewriteEngine On\nRewriteCond %{HTTPS} !on\nRewriteRule (.*) https:\/\/%{HTTP_HOST}%{REQUEST_URI}<\/pre>\n<h3>Activate SSI<\/h3>\n<p>If you want to activate SSI for HTML and or SHTML file types, try:<\/p>\n<pre>AddType text\/html .html\nAddType text\/html .shtml\nAddHandler server-parsed .html\nAddHandler server-parsed .shtml\nAddHandler server-parsed .htm<\/pre>\n<h3>Disable or Enable Directory browsing<\/h3>\n<pre># disable directory browsing\nOptions All -Indexes\n# enable directory browsing\nOptions All +Indexes<\/pre>\n<h3>Change the Charset and Language headers<\/h3>\n<p>For those who want to change the current character set and language for a specific directory use:<\/p>\n<pre>AddDefaultCharset UTF-8\nDefaultLanguage en-GB<\/pre>\n<h3>Block Unwanted Referrals<\/h3>\n<p>If you want to block unwanted visitors from a particular website or range of websites you could use:<\/p>\n<pre>&lt;IfModule mod_rewrite.c&gt;\n RewriteEngine on\n RewriteCond %{HTTP_REFERER} website1.com [NC,OR]\n RewriteCond %{HTTP_REFERER} website2.com [NC,OR]\n RewriteRule .* - [F]\n&lt;\/ifModule&gt;<\/pre>\n<h3>Block Unwanted User Agents<\/h3>\n<p>With the following method, you could save your bandwidth by blocking certain bots or spiders from trawling your website:<\/p>\n<pre>&lt;IfModule mod_rewrite.c&gt;\nSetEnvIfNoCase ^User-Agent$ .*(bot1|bot2|bot3|bot4|bot5|bot6|) HTTP_SAFE_BADBOT\nSetEnvIfNoCase ^User-Agent$ .*(bot1|bot2|bot3|bot4|bot5|bot6|) HTTP_SAFE_BADBOT\nDeny from env=HTTP_SAFE_BADBOT\n&lt;\/ifModule&gt;<\/pre>\n<h3>Block Access to a Comprehensive Range of Files<\/h3>\n<p>If you want to protect particular files, or even block access to the .htaccess file, try customising the following code:<\/p>\n<pre>&lt;Files privatefile.jpg&gt;\n order allow,deny\n deny from all\n&lt;\/Files&gt;\n\n&lt;FilesMatch \".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$\"&gt;\n Order Allow,Deny\n Deny from all\n&lt;\/FilesMatch&gt;<\/pre>\n<h3>And Lastly \u2026<\/h3>\n<p>For reasons of security alone, I think the chance to rename the .htaccess file is very useful:<\/p>\n<pre>AccessFileName ht.access<\/pre>\n<p>In writing this article I have tried to highlight the range of functions htaccess can be used for. Of course, I haven\u2019t\u00a0covered everything but as you can see, .htaccess might be an old tool but it still has an important role to play in enhancing your website.<\/p>\n<p>&nbsp;<\/p>\n<\/section>\n<div class=\"article__author-bios\">\n<div class=\"contributor contributor--large\">\n<figure class=\"contributor_avatar\"><a href=\"http:\/\/www.sitepoint.com\/author\/jhobson\/\"> <img loading=\"lazy\" decoding=\"async\" class=\"avatar avatar-96 photo\" src=\"http:\/\/1.gravatar.com\/avatar\/d30c3553f26458e640c056c6845d85be?s=96&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&amp;r=G\" alt=\"\" width=\"96\" height=\"96\" \/> <\/a><\/figure>\n<div class=\"contributor_details\">\n<div class=\"contributor_name\"><a href=\"http:\/\/www.sitepoint.com\/author\/jhobson\/\">Jonathan Hobson<\/a><\/div>\n<p class=\"contributor_longdesc\">Jonathan is an independent web developer, server administrator and application programmer and for nearly 20 years he has been working behind the scenes to support companies, organisations and individuals from all over the world to realise their digital ambitions. As a practitioner of many the computer languages Jonathan enjoys all things Linux, writing code, building computers, playing the XBOX, history and getting 'out and about&#8217; in the big outdoors. He thrives on new challenges, works around the clock and prides himself on being friendly, honest, reliable and ultimately, the complete professional.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"related-content\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Creating and Uploading an .htaccess File Creating an .htaccess file is very easy. Simply open Notepad or a similar text-based program, switch off word-wrap, add the code and save the file in the usual way. For example, you could call it: htaccess.txt Upload the file to the relevant directory on your web server and then&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/2012\/03\/02\/htpaccess\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14,3],"class_list":["post-812","post","type-post","status-publish","format-standard","hentry","category-bez-kategorii","tag-bezpieczenstwo","tag-internet"],"_links":{"self":[{"href":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/wp-json\/wp\/v2\/posts\/812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/wp-json\/wp\/v2\/comments?post=812"}],"version-history":[{"count":0,"href":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/wp-json\/wp\/v2\/posts\/812\/revisions"}],"wp:attachment":[{"href":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/wp-json\/wp\/v2\/media?parent=812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/wp-json\/wp\/v2\/categories?post=812"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/u239160.webh.me\/jakisproblem.pl\/index.php\/wp-json\/wp\/v2\/tags?post=812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}