{"id":4542,"date":"2016-10-14T13:44:37","date_gmt":"2016-10-14T12:44:37","guid":{"rendered":"https:\/\/www.utilewebsites.nl\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/"},"modified":"2023-04-06T08:04:53","modified_gmt":"2023-04-06T07:04:53","slug":"what-can-i-do-with-a-htaccess-file-common-examples-apache","status":"publish","type":"wz_knowledgebase","link":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/","title":{"rendered":"What can I do with a .htaccess file \/ common examples (apache)"},"content":{"rendered":"<p>  A .htaccess file (with a dot!) is a file type with no name. It is used for Web sites running on an Apache server. You can use it to execute certain things and change or bypass some (server) settings. Below we give you some examples of .htaccess rules that are commonly used. Before some things need to be \"rewritten\" but first the general stuff.<\/p>\n<p><strong>Blocking certain IP numbers<\/strong><br \/>\nIf you want to block an ip number to the site then do so with the following rules:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>order deny,allow\ndeny from 12.34.56.78\ndeny from 78.56.34.12\nallow from all\n<\/code><\/pre>\n<p>Order is : first all deny's are executed and only then the allows. Thus, first the indicated IP numbers are blocked and the others are allowed.<\/p>\n<p><strong>Block access .htaccess<\/strong><br \/>\nTo prevent people from viewing your .htacces, you can block access. You then add the following script:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>order allow,deny\ndeny from all\n<\/code><\/pre>\n<p>The above rules apply specifically to the .htaccess file. Of course, one can also protect other files and folders using the same rules.<\/p>\n<p><strong>Define the index file<\/strong><br \/>\nWhen querying the root or a specific folder, the server will search for the index file. This is the file to be displayed if no file is defined. For example: www.mijndomein.nl\/uwfolder\/. In most cases, servers have the following files set up: index.html, index.htm, default.asp (IIS), index.php (Apache). You can adjust this in the following way<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>DirectoryIndex willekeurig.html\n<\/code><\/pre>\n<p><strong>Custom error messages<\/strong><br \/>\nMost servers have set their default page for error messages. If you want to customize this to your own look-and-feel (html page) of, say, a 404 page, you can do so in the way below :<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>ErrorDocument 403 \/fouten\/geen-toegang.html\nErrorDocument 404 \/fouten\/niet-gevonden.html\n<\/code><\/pre>\n<p><strong>Basic redirects<\/strong><br \/>\nYou can set some basic redirects in .htaccess. You can attach conditions to these or not. For conditions you use regular expressions . Here are some examples<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>Redirect 301 \/uwoudebestand.php http:\/\/www.mijndomein.nl\/nieuwemap\/uwnieuwebestand.php\n<\/code><\/pre>\n<p>Make sure that the second address is a complete address, otherwise this will not work. The 301 is the redirect code for a permanent redirect.<\/p>\n<p><strong>Turn on the rewrite engine<\/strong><br \/>\nThe previous ones were the basic setting. Now the rewrite engine can be turned on. These rules allow you to do a rewrite\/redirect based on some data (hostname, referer, filename, querystring, method). To turn on the engine, use the following line:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>RewriteEngine on\n<\/code><\/pre>\n<p><strong>One host name for your website<\/strong><br \/>\nIt may be desirable for SEO reasons to have your website available on 1 hostname. This will prevent duplicate content from occurring because your website will be indexed on domain A and domain B. Also, you don't want your website to be indexed on both www.site.nl and site.co.uk. To avoid this, add the following lines<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>RewriteCond %{HTTP_HOST} !^www\\.mijndomein\\.nl [NC]\nRewriteRule ^(.*)$ http:\/\/www\\.mijndomein\\.nl\/$1 [L,R=301]\n<\/code><\/pre>\n<p>This indicates: if the hostname is not (the exclamation mark stands for: not) www.mijndomein.nl then a 301 redirect should be done to www.mijndomein.nl. With a few additional settings like [NC], you say the case (upper\/lower case) can be ignored. With [L] you say that this last line should be executed immediately. The [R=301] indicates that it should not be a rewrite but a redirect of type 301 (permanent).<\/p>\n<p><strong>Old domain\/website redirects<\/strong><br \/>\nThe above rules are meant to perform a redirect only if the old domain is linked to the new server in the DNS. As a result, all requests will be handled on 1 server. If this is not possible, you can place a redirect on the old server. This redirect then looks like this :<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>RewriteRule ^(.*)$ http:\/\/www\\.mijndomein\\.nl\/$1 [L,R=301]\n<\/code><\/pre>\n<p><strong>Extreme traffic redirects<\/strong><br \/>\nIt may happen that your website is linked in a place that does not make you happy because it costs a huge amount of data traffic and brings visitors to your website that do not add value. In this case, place some rules to subtly redirect all this traffic:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>\nRewriteCond %{HTTP_REFERER} deverkeerdesite\\.nl [NC]\nRewriteRule ^(.*)$ http:\/\/www.google.nl [L,R=302]\n<\/code><\/pre>\n<p>Visitors who are then directed to your website from deverkeerdesite.nl will now be redirected to Google. This can be just the difference between an overload (server overload) and staying online.<\/p>\n<p><strong>Redirecting from http to https<br \/>\n<\/strong>The code below will redirect all your traffic destined for http: to https:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>RewriteEngine On\nRewriteCond %{HTTPS} off\nRewriteRule (.*) https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]\n<\/code><\/pre>\n<p><em>Note this will redirect using the 301 'permanently moved' redirect, which will help transfer your SEO rankings.<\/em><\/p>\n<p><strong>Rewrite everything but...<\/strong><br \/>\nIn most cases, the rewrite engine is used to rewrite so-called neat urls behind the scenes to 1 file that handles everything else. For example, you can internally rewrite the address \/folder\/map2\/file\/ to index.php?folder\/map2\/file\/:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>RewriteRule ^(.*)$ \/index.php?$1 [L]\n<\/code><\/pre>\n<p>You can attach certain conditions to this: rewrite all urls unless it is an existing folder or file. You can use this to have links to PDF files, for example, not rewritten but simply ignored:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>RewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^(.*)$ \/index.php?$1 [L]\n<\/code><\/pre>\n<p><strong>Rewrite a particular folder<\/strong><br \/>\nYou can also redirect or rewrite 1 particular folder. You then use a condition to limit the rule to 1 folder:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>RewriteCond %{REQUEST_URI} ^\/deze_map\/ [NC]\nRewriteRule ^(.*)$ \/index_voor_map.php?$1 [L]\n<\/code><\/pre>\n<p>and in addition there are the following 2 lines:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>RewriteCond %{REQUEST_METHOD} GET\nRewriteCond %{REQUEST_URI} !^\/nog_een_map\/\n<\/code><\/pre>\n<p>These rules say that the request method must be GET (you can of course use POST here as well) and that the folder \/yet_a_folder\/ can be ignored. Note here the exclamation point which means 'not'.<\/p>\n<p><strong>Multiple conditions<\/strong><br \/>\nIf you want to use multiple conditions, you can header them with the OR:<\/p>\n<pre class=\"lang-php prettyprint prettyprinted\"><code>RewriteCond %{REQUEST_URI} ^\/map1\/$ [NC,OR]\nRewriteCond %{REQUEST_URI} ^\/map2\/$ [NC]\n<\/code><\/pre>\n<p><strong>In conclusion<\/strong><\/p>\n<p>Above we have explained to you some basic redirect and rewrite rules which you can extend and combine where possible. Hoping we have helped you on your way with this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A .htaccess file (with a dot!) is a file type with no name. It is used for Web sites running on an Apache server. You can use it to execute certain things and change or bypass some (server) settings. Below we give you some examples of .htaccess rules that are commonly used. Before some things need to be \"rewritten\" but first the general stuff. Blocking certain IP numbers If you want to block an ip number to the site then do so with the following rules: order deny,allow deny from 12.34.56.78 deny from 78.56.34.12 allow from all Order is :&nbsp;<a href=\"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/\" class=\"read-more\">Continue Reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"wzkb_category":[60],"wzkb_tag":[],"class_list":["post-4542","wz_knowledgebase","type-wz_knowledgebase","status-publish","hentry","wzkb_category-linux"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>What can I do with a .htaccess file \/ common examples (apache) - Utilewebsites<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What can I do with a .htaccess file \/ common examples (apache) - Utilewebsites\" \/>\n<meta property=\"og:description\" content=\"A .htaccess file (with a dot!) is a file type with no name. It is used for Web sites running on an Apache server. You can use it to execute certain things and change or bypass some (server) settings. Below we give you some examples of .htaccess rules that are commonly used. Before some things need to be &quot;rewritten&quot; but first the general stuff. Blocking certain IP numbers If you want to block an ip number to the site then do so with the following rules: order deny,allow deny from 12.34.56.78 deny from 78.56.34.12 allow from all Order is :&nbsp;Continue Reading\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/\" \/>\n<meta property=\"og:site_name\" content=\"Utilewebsites\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-06T07:04:53+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/what-can-i-do-with-a-htaccess-file-common-examples-apache\\\/\",\"url\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/what-can-i-do-with-a-htaccess-file-common-examples-apache\\\/\",\"name\":\"What can I do with a .htaccess file \\\/ common examples (apache) - Utilewebsites\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#website\"},\"datePublished\":\"2016-10-14T12:44:37+00:00\",\"dateModified\":\"2023-04-06T07:04:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/what-can-i-do-with-a-htaccess-file-common-examples-apache\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/what-can-i-do-with-a-htaccess-file-common-examples-apache\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/what-can-i-do-with-a-htaccess-file-common-examples-apache\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Knowledge Base\",\"item\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/knowledgebase\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"What can I do with a .htaccess file \\\/ common examples (apache)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/\",\"name\":\"Utilewebsites\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#organization\",\"name\":\"Utilewebsites\",\"url\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.utilewebsites.nl\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/logo-Utilewebsites-2017.png\",\"contentUrl\":\"https:\\\/\\\/www.utilewebsites.nl\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/logo-Utilewebsites-2017.png\",\"width\":3000,\"height\":593,\"caption\":\"Utilewebsites\"},\"image\":{\"@id\":\"https:\\\/\\\/www.utilewebsites.nl\\\/en\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What can I do with a .htaccess file \/ common examples (apache) - Utilewebsites","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/","og_locale":"en_US","og_type":"article","og_title":"What can I do with a .htaccess file \/ common examples (apache) - Utilewebsites","og_description":"A .htaccess file (with a dot!) is a file type with no name. It is used for Web sites running on an Apache server. You can use it to execute certain things and change or bypass some (server) settings. Below we give you some examples of .htaccess rules that are commonly used. Before some things need to be \"rewritten\" but first the general stuff. Blocking certain IP numbers If you want to block an ip number to the site then do so with the following rules: order deny,allow deny from 12.34.56.78 deny from 78.56.34.12 allow from all Order is :&nbsp;Continue Reading","og_url":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/","og_site_name":"Utilewebsites","article_modified_time":"2023-04-06T07:04:53+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/","url":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/","name":"What can I do with a .htaccess file \/ common examples (apache) - Utilewebsites","isPartOf":{"@id":"https:\/\/www.utilewebsites.nl\/en\/#website"},"datePublished":"2016-10-14T12:44:37+00:00","dateModified":"2023-04-06T07:04:53+00:00","breadcrumb":{"@id":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/what-can-i-do-with-a-htaccess-file-common-examples-apache\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.utilewebsites.nl\/en\/"},{"@type":"ListItem","position":2,"name":"Knowledge Base","item":"https:\/\/www.utilewebsites.nl\/en\/knowledgebase\/"},{"@type":"ListItem","position":3,"name":"What can I do with a .htaccess file \/ common examples (apache)"}]},{"@type":"WebSite","@id":"https:\/\/www.utilewebsites.nl\/en\/#website","url":"https:\/\/www.utilewebsites.nl\/en\/","name":"Utilewebsites","description":"","publisher":{"@id":"https:\/\/www.utilewebsites.nl\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.utilewebsites.nl\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.utilewebsites.nl\/en\/#organization","name":"Utilewebsites","url":"https:\/\/www.utilewebsites.nl\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.utilewebsites.nl\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.utilewebsites.nl\/wp-content\/uploads\/2019\/08\/logo-Utilewebsites-2017.png","contentUrl":"https:\/\/www.utilewebsites.nl\/wp-content\/uploads\/2019\/08\/logo-Utilewebsites-2017.png","width":3000,"height":593,"caption":"Utilewebsites"},"image":{"@id":"https:\/\/www.utilewebsites.nl\/en\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wz_knowledgebase\/4542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wz_knowledgebase"}],"about":[{"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/types\/wz_knowledgebase"}],"author":[{"embeddable":true,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/comments?post=4542"}],"version-history":[{"count":1,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wz_knowledgebase\/4542\/revisions"}],"predecessor-version":[{"id":4543,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wz_knowledgebase\/4542\/revisions\/4543"}],"wp:attachment":[{"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/media?parent=4542"}],"wp:term":[{"taxonomy":"wzkb_category","embeddable":true,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wzkb_category?post=4542"},{"taxonomy":"wzkb_tag","embeddable":true,"href":"https:\/\/www.utilewebsites.nl\/en\/wp-json\/wp\/v2\/wzkb_tag?post=4542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}