{"id":53,"date":"2019-03-10T03:17:44","date_gmt":"2019-03-10T03:17:44","guid":{"rendered":"https:\/\/www.npasson.com\/blog\/?p=53"},"modified":"2019-03-10T11:01:35","modified_gmt":"2019-03-10T11:01:35","slug":"csv-to-htaccess-usage-guide","status":"publish","type":"post","link":"https:\/\/www.npasson.com\/blog\/csv-to-htaccess-usage-guide\/","title":{"rendered":"CSV to .htaccess \u2013 Usage Guide"},"content":{"rendered":"\n<p>I have recently published a new repository, which can be found <a href=\"https:\/\/github.com\/npasson\/csv_to_htaccess\">here<\/a>. It allows you to generate a .htaccess file from a CSV table. This post will help you use the tool.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2>Getting started<\/h2>\n\n\n\n<p>If you don\u2019t have Git installed, now is the perfect time to do so. Just search for \u201c&lt;your operating system&gt; install git\u201d and you\u2019ll find plenty of instructions. If you have Git installed, simply navigate to a folder of your choice and type in<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/npasson\/csv_to_htaccess<\/code><\/pre>\n\n\n\n<p>to download the repository. In case you really don\u2019t want to install Git, you can download the repo as a .zip\/.tar.gz file from the website linked above.<\/p>\n\n\n\n<p>You should have&nbsp;<code>make<\/code> and <code>gcc<\/code> installed. If you\u2019re on Linux, type<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install make gcc<\/code><\/pre>\n\n\n\n<p>and on Windows, install the <a href=\"http:\/\/mingw.org\">MinGW tool chain<\/a>. (you need <code>mingw32-gcc-g++<\/code> and its dependencies).<\/p>\n\n\n\n<p>I will *not* provide a binary file for any of my repositories. The fatal flaw with open source is that nobody actually compiles things themselves, so developers can put anything in a binary. Therefore, to keep the trust of my users, I require all of my repositories to be compiled on their own.<\/p>\n\n\n\n<p>In the root directory of the repo, either enter <code>make<\/code> (on Linux) or <code>mingw32-make<\/code> (on Windows). This will build the binary into a subfolder called <code>bin<\/code>.<\/p>\n\n\n\n<h2>Usage<\/h2>\n\n\n\n<p>There are three different possibilities of usage, each with a few modifications. For sake of argument, let\u2019s assume we own the domain <code>example.com<\/code>.<\/p>\n\n\n\n<h3>Case 1: Simple Path<\/h3>\n\n\n\n<p>Let\u2019s say you want to redirect from example.com\/foo to example.com\/bar. The CSV line for this redirect couldn\u2019t be simpler:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>foo<\/td><td>bar<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>That\u2019s it. Note three things:<br><\/p>\n\n\n\n<ol><li>This will catch the following requests:<ul><li>example.com\/foo<\/li><li>example.com\/foo\/<\/li><\/ul><\/li><li>The slashes for the FROM column do not matter. <code>foo<\/code>, <code>\/foo<\/code>, <code>foo\/<\/code> and <code>\/foo\/<\/code> are treated the same.<\/li><li>This will redirect *exactly* to <code>example.com\/bar<\/code>. There will be no slash at the end. If you need it, add it in the CSV.<\/li><\/ol>\n\n\n\n<p>The resulting redirects are 100% portable, so you can use example.test locally and example.com in production and won\u2019t need to change or regenerate the file. <\/p>\n\n\n\n<h3>Case 2: Full Paths<\/h3>\n\n\n\n<p>There\u2019s a different way to generate the above redirects, if you don\u2019t care about simplicity. You can copy-paste the full URL into the CSV, like so:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>https:\/\/example.com\/foo<\/td><td>https:\/\/www.example.com\/bar<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>This will generate the exact same redirect as above. And even better, <em>it\u2019s still portable<\/em>! This means if you discover a needed redirect in testing, you can copy-paste your example.test-URLs in-between the production URLs and it won\u2019t hurt your result.<\/p>\n\n\n\n<h3>Case 3: External redirects<\/h3>\n\n\n\n<p>Sometimes, you want to redirect to a different page. Maybe you want to redirect people to the German site when visiting \/foo\/. This can be accomplished:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>foo<\/td><td>https:\/\/beispiel.de<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The rule for this is simple: If there\u2019s no domain on the left and there\u2019s one on the right, the redirect will be treated as external.<\/p>\n\n\n\n<p><strong>Warning<\/strong>: External redirect targets are hardcoded. If you\u2019re redirecting to beispiel.test in your dev environment, you will have to regenerate the file for production release.<\/p>\n\n\n\n<p><strong>Warning<\/strong>: External redirects need a protocol. Otherwise they will be treated as documents. For example:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>foo<\/td><td>beispiel.de<br><\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>This will redirect example.com\/foo to example.com\/beispiel.de.<\/p>\n\n\n\n<h3>Mixed Case 1: Full Path + External<\/h3>\n\n\n\n<p>You can easily use full paths for external redirects:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>https:\/\/example.com\/foo<\/td><td>https:\/\/beispiel.de<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The rule for this is: If the domains (+protocol!) are different, the redirect is treated as external.<\/p>\n\n\n\n<p>This will generate exactly the same redirect as in case 3. The FROM field will also be portable (example.com\/foo works just like example.test\/foo), like in case 3.<\/p>\n\n\n\n<h3>Mixed Case 2: Simple + Full Paths<\/h3>\n\n\n\n<p>Sometimes you just want to redirect many paths to the root page, but still use full paths for convenience. This works:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>https:\/\/example.com\/badsite<\/td><td>root\/<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>The target field can be a simple path, which yields the same results as in case 1 and 2.<\/p>\n\n\n\n<h2>Pre\u2013 and Suffixes<\/h2>\n\n\n\n<p>In many development environments, the actual redirects aren\u2019t the only thing in a .htaccess file. That\u2019s why you can import pre\u2013 and suffixes.<\/p>\n\n\n\n<p><strong>Note<\/strong>: If a prefix is given, the generator will omit <code>RewriteEngine On<\/code> from the generated file as it assumes it to be in the prefix.<\/p>\n\n\n\n<p>Prefixes and suffixes are simply blocks of text that are inserted before\/after the generated redirects, and are extracted from files. The files to extract them from are given with the optional <code>--prefix &lt;file&gt;<\/code> and <code>--suffix &lt;file&gt;<\/code> parameters. They\u2019re enclosed by <code>#BEGIN PREFIX<\/code> and <code>#END PREFIX<\/code> (or the suffix equivalent) comments.<\/p>\n\n\n\n<h2>Support<\/h2>\n\n\n\n<p>There is no official support. If you think you found a bug, please raise an issue on GitHub.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have recently published a new repository, which can be found here. It allows you to generate a .htaccess file from a CSV table. This<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.npasson.com\/blog\/csv-to-htaccess-usage-guide\/\">Continue Reading<span class=\"screen-reader-text\">CSV to .htaccess \u2013 Usage Guide<\/span> <i class=\"fas fa-angle-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":""},"categories":[3,2],"tags":[6],"featured_image_src":null,"author_info":{"display_name":"npasson","author_link":"https:\/\/www.npasson.com\/blog\/author\/npasson\/"},"_links":{"self":[{"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/posts\/53"}],"collection":[{"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":6,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":60,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions\/60"}],"wp:attachment":[{"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.npasson.com\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}