Categories
apache Apache mod-rewrite and htaccess related Articles htaccess

Apache .htaccess tutorial for beginners | Learn URL rewriting and basics of mod_rewrite – helponnet.com

htaccess has always been a confusing topic for all Apache users especially for newbies who are learning to write RewriteRules to modify URLs via an htaccess file. The reason why some people find it confusing is because the Apache mod-rewrite documentation is hard to follow and there are no easy to follow htaccess step by step tutorial available on the internet.
I often see users struggling with their RewriteRule code on StackOverflow and I help them fix issues when I can.

In this 10 minute read article I am going to post an htaccess short step by step tutorial for people who wish to learn some basics of URL rewriting on Apache server.
If you follow this tutorial from top to the bottom then I am sure you will learn alot about URL rewriting and you will have some basic idea to create your own RewriteRules.


What is an .htaccess file?

What is an htaccess file

It’s a directory level configuration file used by Apache web server to modify URLs , directory appearance and other server settings.

.htaccess is a dot prefix file.
The name starts with a single dot . and ends with htaccess .

You can change the name to make it look something like htaccess.txt from your server.config file.
The benefit of using an htaccess file is that it’s easy to edit and available on all types of hosting services.

The downside of using an .htaccess file is that it slows down your server performance as it’s read on each request but that’s not going to be a big issue for you.
You can see the world’s largest CMS WordPress also uses an htaccess file to shorten the blog URLs.
An htaccess file is easy to maintain and set-up that’s why many web developers prefer to use it rather then editing the main configuration file.

Apache mod-rewrite

Rewrite module aka mod-rewrite is a powerful Apache module that provides URLs rewriting functionalitis to Apache users.
The official documentation of mod_rewrite is available on Apache mod_rewrite.
Mod-rewrite provides URL rewriting directives such as

RewriteEngine
RewriteBase
RewriteRule
RewriteCond
All these directives are used in an htaccess file to manipulate incoming URL requests and other settings on the server.

You will learn more about these directives later in this article but for now just focus on what the Rewrite module is and how to enable it.
Almost all live web hosting providers will provide you Apache server with pre-installed mod-rewrite. But in some rearest cases if it’s not already installed then you can ask your service provider to enable it for you or you can also enable it yourself by modifying the server configuration file that’s located in /etc/httpd/conf/httpd.conf .
Open this file for editing and replace the line AllowOverride none to AllowOverride all this will then make it possible to use mod-rewrite in an htaccess file.

To verify whether your server supports mod-rewrite in htaccess , you can do the following :

  • Create an .htaccess in your public_html folder.
  • Put some random texts to your htaccess file like “fooooobarrr” .
  • Now, visit any URL on your site.

If it generates a 500 internal server error then the mod-rewrite is enabled on your server otherwise if you don’t get any error then you will need to enable this using the easy method I mentioned above.

Let’s create our first Hello-world RewriteRule

If you are learning to write htaccess rules while following this article , then I suggest you start learning with an empty htaccess file as with an empty Htaccess you can delete the code if something goes wrong or you want to test a different code, this will improve your learning experience.

Create an htaccess file on your document root and put the following contents in that file :

RewriteEngine on
RewriteRule ^helloWorld$ /index.php

Save the htaccess file and type the following URL into your browser address bar :
https://yourdomain.com/helloWorld
You will see the real rewrite mazic happening now.
The rule will internally map your URL path /helloWorld to the destination file /index.php.
The redirection happens behind the screen. You will see contents of the index.php file on the /helloWorld path.

Congratulations , you have written your first helloWorld rule successfully.
Now let’s understand how the code above works. Let’s understand it line by line.

  • RewriteEngine on : This directive turns on the engine for rewriting URLs.
    This directive should be placed once at the top of your htaccess.
    There is no need to add “RewriteEngine” with each rule, just one single line at the top is enough.
  • RewriteRule ^helloWorld$ /index.php : This is a RewriteRule directive provided by mod-rewrite. This line rewrites the request from /helloWorld to /index.php .
    You might have noticed one thing that our Rule’s pattern doesn’t start with a leading slash / .
    This is because RewriteRule uses a relative path in its pattern so ^/helloWorld$ will not work in htaccess but in a server.config file.

Okay, now I hope you have had some basic idea about how to create a simpe RewriteRule.
Now we will learn about other parts of a RewriteRule.

Mod-rewrite Flags

Flags are optional in RewriteRule but you will need to use them in most cases.
Flags provide additional instructions to a RewriteRule.
For example a RewriteRule with [R] flag tells the rule to make an external redirection of URLs.

See a list of most commonly used flags in mod-rewrite .

Flags are used inside sequere brackets [] .
You can use multiple flags with a RewriteRule seperating them by a comma char ,.

Flags are optional in RewriteRule but you will most often need to use them in Rule to change the URL rewriting behaviour.

You will needv to use [NC] flag in the following Rule:

RewriteRule ^foobar$ /index.php

NC stands for NoCase, this flag is used to match both uppercase and lowercase chars in URI.
With NC, the rule above will match the following both URIs :


/foobar

and
/fOObAr
otherwise without this flag, by default RewriteRule only matches the case sensitive URI string.

You can learn more about flags on this post .

Conditions in RewriteRule

Conditions in Apache mod-rewrite

You can use conditions with your RewriteRule to rewrite or redirect your site URLs based on some specific conditions.

RewriteCond directive is used for this purpose.
With RewriteCond you can rewrite your URL based on host , https , request URI headers.

Check this post to learn more about using RewriteCond directive.
You can use if/else logic for RewriteRule .

The following Rule uses RewriteCond to redirect the URL based on host header. The condition used in this rule can also be read as
“If host ==example.com then Redirect .

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^foobar$ /index.php [R]

This rule will redirect /foobar to /index.php if the condition is met.

You can use multiple conditions with a single RewriteRule .

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^foobar$ /index.php [R]

The conditions above uses [OR] flag. This makes one of the conditions optional. So you can read it as :
“If host ==example.com OR host==www.example.com” then Redirect.

To redirect http URLs to https , you need a condition logic. That checks if the incoming URL is using http scheme , then redirect the URL to use https :

RewriteEngine on

RewriteCond %{REQUEST_SCHEME} ^http$
RewriteRule (.*) https://example.com/$1 [R]

I hope now you have had some basic idea of how conditions work and how to use them with RewriteRule.

Regex in mod-rewrite

RewriteRule regex

Mod-rewrite directives RewriteRule and RewriteCond use a regular expression based pattern that makes the match easier.
With a Regex based pattern a single RewriteRule can match multiple URIs.

If you want to learn Regex , you can follow this tutorial .

The following rule with regex pattern can either match /helloWorld or /hello ladies :

RewriteEngine on
RewriteRule ^(helloWorld|hello ladies)$ /index.php

The match is saved in $n variable . Since we have one capture group in the rule above , the match is saved in $1 .

You can use the matched value in the destination string of RewriteRule.

RewriteEngine on
RewriteRule ^(helloWorld|hello ladies)$ /index.php?q=$1

The rule above rewrites /helloWorld to /index.php?q=helloWorld

You can use Regular expressions with RewriteCond :

RewriteCond %{HTTP_HOST} ^(www\.)example.com$

The condition above checks both www and non-www domain .

Isn’t it easier with Regex?

Comments in htaccess code

In any programming languages , comments are used to explain the block of code. Like in PHP we use # to write a single line comment and /*…*/ for multiple lines.
In htaccess , you can only comment out a single line using the hash character # .

Anything that goes after # is treated as a comment in htaccess.

#This is a comment.
#This is another comment.

To explain an htaccess code you can start your comment with a # character but keep in mind that the # is a single line comment. To add multiple line comments you will need to start each line with a hash character like the example shown below

#redirect http to https
#This rule also reditects non-www to www at the same time
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www
RewriteRule (.*) https://www.example.com/$1 [L,R]

Error handling with htaccess

With an htaccess file you can handle server errors with just a few lines of code.
On Apache there are different types of server errors . Some most common errors are :

  • 403 (Forbidden error)
  • 404 (Not found error)
  • 410 (Gone)
  • 500 (Internal server error)
  • 503 (Server unavailable) .

With an htaccess file you can easily handle these server errors using ErrorDocument directive which is part of Apache core module.
403 is the error status code which represents a “Forbidden error” (Access to the resource is denied) .
To redirect your site URLs to a different location when this error occurs, you can use the following code in htaccess :

ErrorDocument 403 /403.php

Here 403.php is the file that is called when you request something that is protected on your server.

The code below handles 404 “not found” URLs.

ErrorDocument 404 /404.php

The same code format is used to handle other server errors except 500 (Internal server error) . You need to handle it from your main server configuration file.

RewriteRule code examples

WWW to non-www

With the following rule you can redirect your www subdomain to root domain :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]

Redirect non-www to www (naked domain to www subdomain)

If you want to redirect your root domain to www subdomain , here is the rule you can use to enforce www on your website :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

Change directory index file

By default , your server shows index.html , index.php or whatever index file exists to client when a directory is direct accessed , but you can change this behaviour by simply using the following line in htaccess , read more about DirectoryIndex

DirectoryIndex somefile.php

Access all files without adding an extension at the end

If you need visit your file URLs without adding extension at the end , for example : to visit /file.php as /file you can use the short code below in your htaccess :

Options +Multiviews

Enforce SSL

You can redirect your insecure traffic (http) to the secure version (https) using this rule :

RewriteEngine on
RewriteCond %{HTTPS} off [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]

Add a trailing slash

To add a trailing slash to your URLs , you can use the following :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !/$ %{REQUEST_URI}/ [R,L]

Allow non-php extension

If you want to access your .PHP files without using extension at the end , you can use :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RwriteRule ^(.+?)/?$ /$1.php [L]

More rules : 5 Awasome htaccess rules you can just copy & paste .

.htaccess tutorial pdf

This htaccess tutorial is available for download as PDF format. You can Download this htaccess tutorial as .pdf and use it for personal and educational purposes only.

I hope this tutorial was helpful. If there is anything that you couldn’t understand or you need my help with htaccess , you can post a comment bellow.

Thank you so much reading this.

Categories
AndroidStudio

How to use Android studio on low-end machines 4GB 8 GB of RAM

Android studio on Windows PC

A question that I often see users asking on the internet is “Does Android studio really work on 4 or 8 gigabyte RAM machine” , “Android studio system requirements”
.

The short answer is Yes . It works but it depends on the Oprating system your machine has.

According to the official Android studio webpage :
Android studio system requirements
The minimum RAM required to run Android studio is 4GB. And the recommended RAM is 8GB for Windows, Linux ,Chrome OS and other operating systems .

Android studio on 6GB RAM

Android studio is used for Android apps development. To develop and design an Android app we need to set up Android environment on our system.

I know many of us who are curious about how this software works have searched online for the informations but the thing is we sometimes get confusing informations on the internet and that really demotivates us.

I am not an expert in Android development. I am a medium level user like many of you who knows how to create a basic App
from the scratch so in this post I am going to share my personal experience with Android studio software. I will share with you my Operating system details and how I managed to turn my low-end PC into a Android development machine.

I use 6GB RAM PC for Android studio

The installed RAM in my machine is 6GB and it’s Windows 8. The processor is Intel i5 64bit fourth generation.
The following is the screenshot of My PC configurations :

My Asus PC system configuration

My PC has a 64 bit Intel i5 processor 1.6Ghz clock speed. The hard drive capacity is 500GB .

The performance is much better when you are not doing any heavy tasks such as playing games and editing photo/videos.

Like others, I was so excited to install the Android studio Software on my system. I went to the download page and installed the latest Android studio version on my 64bit machine.

The installation took quite a long time , 15 minutes in my case and when everything was ok I tried to open the software but unfortunately it crashed my system. The software took 10 minutes to open and the PC hanged.

It was really frustrating . I somehow managed to bring my system back to normal by shutting down and restarting the PC.

I opened the “create a new project ” tab and created a basic app but again the system hanged when I tried to run the emulator to test the project app.

I know many of you might have experienced this kind of issue on a low-end PC . Android studio works but you can’t run the emulator as it consumes so much system RAM.

Another thing that most people don’t talk about is the PC storage system. The performance of a A PC with hard drive is always slower then the PC with a SSD (Solid state drive) .
Android studio is not that heavier software itself but its emulator takes about 60% of the RAM in your system and that’s the main cause of the system failure.

I have managed to use Android studio on my 6GB and 4GB RAM machines with upgrading a few things on my systems.

I upgraded my hard drive to SSD (Solid State Drive)

Hard drive to SSD for Android studio

My system with a regular hard drive was performing really slow so I had to replace it with 1TB SSD .
A 1TB SSD these days is a bit expensive if you buy it from a reputed Brand .

I paid $120 for this upgrade.
The reason why I had to upgrade to SSD is because of the fact that Hard drive slows down the system performance.
Hard drive works on a rotational machenism. The data is read an written based on the disk rotation. SSD on the other hand doesn’t contain any rotating system to store or read the data as it’s done electronically.
After upgrading my machine to SSD , My system performance changed and read and write speed of my PC was 100 times better then before.
Heavy softwares like filmora and Android studio took a just few seconds to open on the screen .
My system didn’t hang for Android studio after doing the hard drive change. But to use the Android studio smoothly on A 6 GB RAM PC I did another change on my system.

You can buy SSD (Solid state drive) online from Amazon from the link bellow. The Crucial SSD is less expensive and costs less then 3K for 240GB storage.

I use my real Android device as emulator

PCs with 4 , 6 and 8GB ram are still considered to be Low-end machines even if your system has a SSD instead of HDD.
You can not play games.
Some heavier softwares like Android Studio can not work properly especially if you are using Windows.
To use the Android studio smoothly on my windows machine, I use virtual device instead of Android Emulator to run my test apps.
With a virtual device, I can use the Android studio without any system performance error.

Minimum System requirements to run Android studio on Windows and Linux

My experience with Android studio has really been awasome. I started with a 4GB RAM on Windows and upgraded HHD to SSD. I learnt alot about Android studio system requirements along the way.
If you follow Android studio system requirements manual and start with a minimum 4GB RAM machine then you are going to face a lot of issues in the beginning.

The minimum RAM 4GB as suggested by Android studio isn’t actually the minimum and the recommended 8GB shouldn’t be recommended . You can read it as 8GB minimum , 16GB recommended.

Android studio works perfectly fine on PCs with 16 to 32GB RAM.
You won’t need an AVD but instead you can use Android Emulator without any problems on 16 to 32 GB RAM machines.

Windows versions for Android studio

To use Android studio on Windows PC , you must have one the following operating system versions :

  • Windows 7
  • Windows 8
  • Windows 10

And the minimum screen resolution should be 1200*800 .

PS:
Graphic card is not really required for Android studio.

Categories
Apache mod-rewrite and htaccess related Articles

Redirect domain with homepage exception htaccess

In this article I will show you how to write a RewriteRule for htaccess to redirect a website to another website with homepage exception.
I will show you a RewriteRule that does a 301 redirect from your website to another external site. The redirection will get applied to all URLs on your website except the homepage /.

If you are new to htaccess , you can follow our basic htaccess guide .htaccess tutorial for beginners .

Before we get to the rule, let’s first understand the requirement :
Suppose, we have two domains

  • example1.com
  • example2.com

Both domains are on different webservers, and we want that all traffic from example1.com should get redirected to example2.com with homepage exception meaning that the “example1.com/” will not get redirected to example2 domain while all other URLs ie: “example1.com/*” will use the redirection.

There are two different ways to do such redirection with an htaccess file.You can either use mod-rewrite or mod-alias. These two Apache modules can be used for URL redirection on Apache.

Redirect domain with homepage exception using RedirectMatch

One of the easiest way to do this type of redirection with htaccess is a simple one liner RedirectMatch directive. This directive is part of Apache mod-alias. You can use the code below in your htaccess file :

RedirectMatch 301 ^/(.+)$ https://example.com/$1

RedirectMatch directive use regular expression based pattern so the match becomes easier. The code above will redirect all requests except the homepage from your example1 domain to the another domain.

Note : The code should be placed in root htaccess in your example1 domain.

If you want to remove the homepage exception, just change the regex pattern to ^/(.*)$ .

Redirect domain with homepage exception using RewriteRule

You can redirect your domain with RewriteRule directive. RedirectMatch can fail to work if your htaccess contains other directives, in that case RewriteRule is the solution. Just place the code at the top of your htaccess file :

RewriteEngine On
RewriteRule ^(.+)$ https://example2.com/$1 [R=301,L]

In case your destination domain is on the same server and pointing to the same document root then both the Redirects I showed above might cause an infinite redirect error. Use the following rule if both domains are on the same server :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com$
RewriteRule ^(.+)$ https://example2.com/$1 [L,R=301]

I hope this article was helpful, if it really helped you and you want to say thanks just click on the coffee button below to buy me a coffee. Thanks.

Categories
Apache mod-rewrite and htaccess related Articles

How to set subfolder as root folder with htaccess

This article explains how to set a subfolder as root folder for your domain name using RewriteRule directive in an htaccess file.

With an htaccess file , you can easily manage your site URLs the way you want to. You can shorten a long URL, you can completely change the appearance of your site URLs and you can do even more.
If you are new to htaccess and URL rewriting , I have written a Short htaccess tutorial for beginners that you might find interesting and helpful.

This post will show you how to set your subdirectory as main directory using a few lines of htaccess code.
Assuming your folder structure is as follows :

  • /root
  • /subfolder 1
  • /subfolder 2
  • index.php
  • .htaccess

As you can see we have two subfolders in the root directory , to access files or folders inside subfolder1 we have to type https://example.com/subfolder1/file and to access any files/folders in root we just type https://example.com/file .

Okay ,
if you want to set your subfolder1 as your main directory, by setting “subfolder1” as main directory here I mean to say your subfolder will be treated as your root folder. Any requests you receive on your domain via root folder will be mapped to the subfolder1.

The following basic htaccess rule rewrites your root folder and any root requests to /subfolder

RewriteEngine On
#rewrite root to subfolder
#including all files and folders
#exclude the destination folder
RewriteCond %{REQUEST_URI} !^/subfolder1 [NC]
RewriteRule ^(.*)$ /subfolder1/$1 [L]

Okay, isn’t it as simple as ABC?

The rule does the following :

It redirects any files and folders from root to subfolder.
The redirection is internal so your browser won’t redirect and your visitors will not be able to notice it.
The URL example.com/file.php will show you the contents of example.com/subfolder1/file.php
.

Some important things to take note of in the rule :

  • We excluded the destination folder using RewriteCond directive as it’s important to avoid rewrite loop error.
  • We used ^(.*)$ instead of ^(.+)$ in the pattern above so that any empty URI (Root folder “/”) can also map to the subfolder1.
  • Use the rule in your root htaccess only and remove your subfolder1 htaccess if it exists.

Okay, the rule above does its job perfactly but there is still something important to implement. You now have two URLs pointing to the same location, ie . example.com/file.php and example.com/subfolder1/file.php both URLs will show you the same content which can cause duplicate content issues on search engines.
If you care about your site SEO, you will need to fix it. You can set up a 301 redirect from your subfolder1 URLs to the root so that search engines can index only one URL.

The following is the complete version of the htaccess rules to map root to subfolder

RewriteEngine On
#301 Redirect subfolder1 to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^subfolder/(.*)$ /$1 [L,R=301]
#rewrite root to subfolder
#including all files and folders
#exclude the destination folder
RewriteCond %{REQUEST_URI} !^/subfolder1 [NC]
RewriteRule ^(.*)$ /subfolder1/$1 [L]

I hope this article was helpful. If you still need any help regarding root to subfolder redirection , just leave a comment below .I would love to help you out with this. Thanks.

Categories
Apache mod-rewrite and htaccess related Articles

How to remove folder name from URL with htaccess

The most common use of an htaccess file is to shorten and rewrite URLs so that they can look clean and preety.
Short URLs are often easy to remember and type in a web browser. So instead of typing a long URL you can just use its clean version to go to the original page.

In this article , I am going to show you how to shorten a directory URL with htaccess.

Assuming our directory structure is as shown in the pic below :
folder structure

As you can see above, we have a subfolder named “folderA” in the root directory. The subfolder has 2 files

  1. index.html
  2. index2.html

To access the files by a web browser , we have to type https://example.com/folderA/index2.html .
This is the actual path location to visit the file but if you are not happy with the URL and want to shorten it, say you want to remove the folder name , you can easily do this with
RewriteRule directive.
After removing the folder name from URL , the URL to the file will look something like :
https://example.com/index2.html.

In the next section below, I will show you how a folder name can be removed from URL using htaccess.

Removing folder name from URLs with htaccess file

It’s not a rocket science to remove the folder name from URLs. You can easily do this with just a few lines of code in an htaccess file.
Here I am taking the exect same URL example I mentioned above.

To remove the folder A you can use the following lines of code in your htaccess file . Remember to place the code at the top of your htaccess in root.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) /folderA/$1 [L]

That’s it.
With those lines set in your htaccess file, you will be able to access your index2.html file in “folderA” by visiting example.com/index.html . It makes it as if you requested the file from root folder.

The rule above doesn’t remove folder name from the URL , it just makes it so that you can use the new version to access the file but the file can also be visited by typing the original location. This leaves you with two different URLs pointing to the same location. If you want to remove the folder name completely , you can use the following rule instead :

RewriteEngine on
RewriteRule ^folderA/(.+)$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) /folderA/$1 [END]

I have also written an htaccess tutorial for people who are new to htaccess and wish to learn , if you are interested, you can find the tutorial On this link
I hope the post was helpful. Thanks for reading it.

Categories
YouTube

How to get watch hours fast- YouTube watch hours increase helpful trick

There was a time when YouTube users only needed 1K subscribes to get their channel monotised .
“Watch hours” wasn’t the issue only 1K subscribers cap was required but now
on YouTube you also need 4000 watch hours in order to be able to monotise your channel.
4000 watch hours means 240,000 minutes which seems quite complicated to a new youtuber.
Check out this official guide on YouTube monotisation policy

As a new youtuber , if you recently started your channel , and you get upto 500 views daily , it can take a long time upto 3 years to reach the 4K watch hour milestone.
Many new youtubers who want to get their channel monotised fast spend money on ads and paid promotions of their channel. Paid promotion is a good choice but you will have to spend thousands of dollars to reach the milestone.
In this article , I am going to share a free YouTube watch hour trick with you. Although this trick is not officially supported as it goes against the YouTube law but you can try this free trick to increase your watch hours fast on your own risk and learn how the process works.

Increase YouTube watch hours fast trick

What you need to get started

  • PC or Laptop
  • Fast internet connection (4G recommend)
  • Playlists on your YouTube channel

These are all the things you need to get started with this free watch trick.

You must have a pc or laptop and fast internet connection .

Getting started

To get started, open YouTube.com on your desktop web browser in incognito mode. Search your channel in the YouTube search box.
YouTube on laptop browser

Once your channel appears in search, click on it to find your YouTube playlist.
YouTube himfairy
Click on any of your playlist to play it on the browser.

On desktop browser, you can open multiple tabs and play the playlist in each tab.

Next, open the new tab and search your channel again . Play the video playlist in the next tab. You will note that now your playlist is being played in two tab at the same time. You can open multiple tabs like this and play the videos.
To reduce the play time , just increase the playspeed to 2X and set the video quality to 144px as this will consume less data .

You can open upto 20 tabs at the same time in Chrome browser and play process will continue in each tab. By doing this you can increase your watch hours fast and easily . If you play your videos in 20 tabs at the same time , in just 30 minutes you can get upto 200 hours of watch time .

With the trick mentioned above anyone can reach the 4k watch hours milestone in just 15 days but as I already mentioned this trick is neither officially supported nor any professional youtuber will recommend you to do so as it can put a negative impression on your overall performance. You can try this just for testing on your own risk.

Categories
Apache mod-rewrite and htaccess related Articles

Block a specific http referer using RewriteRule in htaccess

HTTP_REFERER header contains URL of the site where the visiter came from. If you came to this site from Google search the domain in the REFERER header will be google.com .
REFERER header contains the full URL string excluding the fregment part which isnt sent to the server.
With mod-rewrite you can manipulate the referer header using %{HTTP_REFERER} variable in RewriteCond directive.
This article shows how you can block access to a specific http referer using mod_rewrite in htaccess file .

Block a specific http referer using RewriteRule in htaccess

I am going to write a sample rule here to block access to example.com domain. You can modify the rule and change the referer domain to whatever domain you want to disallow access to .
The following rule completely blocks example.com . This won’t allow any referer URLs from the example.com domain to your site.

RewriteEngine On
RewriteCond %{HTTP_REFERER} example\.com [NC]
RewriteRule .* - [F]

Explaination :

  • RewriteEngine On tells server to turn on the engine for rewriting URLs. You can remove this line if it’s already present in your htaccess file and put the other two lines just bellow that.
  • RewriteCond %{HTTP_REFERER} example\.com [NC] this creates a condition under which the rule executes . This says “if the refer header is = example.com” . If the condition is met then it sends a green signal to the RewriteRule line that follows it to run and if it’s not met then the rewriting is skipped and the rule is not triggered.
  • RewriteRule .* – [F] rewrites any requested path to F . The F represents a 403 forbidden error. The rule rewrites any request to a 403 error if the condition above it is met.

The RewriteRule posted above in code block blocks the entire example.com referer site meaning that no referer URLs from that domain to your site will be allowed. Clients will get a 403 forbidden error. However if you do not want to block the entire site but a specific URL or page, you can use the following in your htaccess file :

Block a specific referer URL with htaccess

RewriteEngine On
RewriteCond %{HTTP_REFERER} example\.com/thispage\.php [NC]
RewriteRule .* - [F]

This blocks access to a specific referer page example.com/thispage.php while other referer URLs from the domain will keep working normally.

Categories
adsense

Adsense CPC by country on a tech niche blog – CPC by country latest report

CPC stands for Cost Per Click. It’s the revenue you earn as a publisher when someone clicks on your ad. The Cost Per Click for any ad is determined by advertiser . Some advertisers pay more per click then others depending on what type of ad they are advertising.

The revenue from CPC you earn as a publisher varies, depending on the following factors :

  • The country of visitor
  • Type of Adsense ad
  • Demand and supply
  • Type of publisher . Large/Small.
  • Number of bidders for Ad spot.
  • Bid amount

Adsense Cost Per Click depends on the following few factors :

  • Location of visitors : Where you are getting the ad click from. Indian consumer is far less likely to buy then a US consumer. That’s one of the many reasons why Indian CPC is ten times lower then a CPC from US and other developed countries.


  • How competitive is the market?
    Higher the revenue, higher the competion. Example: Insurance CPC is ten times higher than tech.
    In the US , the maximum CPC revenue paying niche is Insurance that can upto $50 per valid click.

  • How many ad networks are bidding on your inventory? Higher the demand, higher the CPC/revenue.
  • Bid amount .
    How much money advertisers are paying for a keyword. Competition among advertisers grow as the number of advertisers willing to buy a keyword go up. Consequently the bid amount also tend to go up and you get high CPC.

CPC by country latest report

I have collected this data from my own Adsense dashboard connected to this blog. This is the real 30 days Adsense CPC report. The reason why I am posting it here is because I want everyone who is interested to have a basic idea of how much Adsense pays per click on a tech niche blog.

Here is a list of a few countries I got clicks from in the last 30 days.

Ordered by CPC

Country Cost Per Click
United States $1.20
Germany $0.32
United Kingdom $0.30
Denmark $0.22
Nigeria $0.22
Span $0.22
Italy $0.19
Canada $0.18
Singapore $0.18
Czechia $0.15
Ukraine $0.11
Australia $0.11
Argentina $0.09
Oman $0.07
Kuwait $0.06
India $0.05
Israel $0.05
Cambodia $0.05
Poland $0.04
Malaysia $0.04
Egypt $0.04
Belgium $0.03
Indonesia $0.03
Tanzania $0.02
Namibia $0.02
Bangladesh $0.01

As you can see the highest CPC I got in the last 30 days is $1.20 and it came from the United states.
Germany is the second country in the list that provided me high CPC with $0.32 and then all other countries are below in the list above.
The lowest CPC came from Bangladesh which is just $0.01. In the last 30 days I got 15 click from Bangladesh and 32 from the United states . With higher CPC from the US my Avarage CPC is $0.41 which in my opinion is a good avarage CPC rate for a tech niche site.

Categories
Apache mod-rewrite and htaccess related Articles

How to set cookies for a specific http_host with htaccess?

This article explains how to set an http cookie for a specific http_host header with htaccess file using mod_rewrite.
With mod_rewrite we can set cookies using [CO] flag.

CO is used to set cookies in htaccess when a specific RewriteRule or the rule containing “CO” flag matches.

In this article , I will show you how we can set cookies when a specific http host (domain) is requested.

Set cookie for specific host header

The solution being provided here is useful if you have multiple domains pointing to the same document root and you want to set cookies for a specific one.

Consider the following example :
This sets a cookie with the following name and value :

cookie name : mydomain
Value : example.com .
The cookie is set only if the rule matches. The rule matches if the condition is met. Here we are using the %{HTTP_HOST} condition to check the current domain name.

RewriteEngine On
#if the host = example.com
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
#run the rule and set cookies
RewriteRule .* - [CO=mydomain:example.com,L]

The rule above sets the cookie named mydomain and value example.com . You can access the cookie in your PHP script or use a rule in htaccess to check the cookie value. The rule above is not generic ,if you don’t want to set the cookie value manually , you can use the following rule instead as it sets the value of cookie automatically from the host condition

RewriteEngine On
#if the host = example.com
RewriteCond %{HTTP_HOST} ^(example.com)$ [NC]
#run the rule and set cookies
RewriteRule .* - [CO=mydomain:%1,L]

The rule examples above set the cookie successfully but there is still one important thing missing in the both examples. We forgot to set the cookie expire time or how long the cookie will survive. We need to add the validity argument to our code . The default cookie expire time is 0 which means the cookie will expire as soon as the browser window is closed.

You can set cookies expire time in minutes starting from 1. To set the cookie validity upto 1 hour we can use 60 as the validity argument.

RewriteRule .* - [CO=mydomain:%1,60,L]

Related article : How to set cookies with mod_rewrite .

Categories
Apache mod-rewrite and htaccess related Articles PHP

How to increase php upload_max_filesize with htaccess?

In PHP the default value of upload_max_filesize is 2MB and the default value for post_max_size is 8MB on the server but you can increase or decrease this size limit by modifying your php.ini or . htaccess file using post_max_size and upload_max_filesize .
You can manually adjust the post data size and file size using these directives in your php.ini. See a list of supported directives you can use in php.ini .
Also see How to increase upload_max_filesize in PHP an article on tecmint.com.

  • post_max_size directive represents the maximum data size your form can submit to server using the POST method.

    The following line in php.ini will set the maximum post size to 500mb which means that your form can submit data upto 500mb in one request.
    post_max_size = "500M"

  • upload_max_filesize represents the maximum filesize your form can submit to server. For example if this is set as the following in your php.ini :

    upload_max_filesize = "250M"

    Then you can upload a file that is upto 250MB in size to server . This won’t allow you to upload files that are greater then 250MB as it’s the maximum size limit set in the directive.

You can easily increase the file upload limit on php by modifying your ini file but not all hosting service providers allow you to do that. If you can not modify your php.ini then the another easy option is to edit it via the htaccess file.

Increase php maximum upload filesize by htaccess

Since htaccess is available on all types of hosting providers, you can easily edit your htaccess file to update the PHP filesize limit.
Add the following two lines to your htaccess . You can change the size according to your needs

php_value post_max_size 100M
php_value upload_max_filesize 100M

This will set the maximum size to 100MB for both . You can change the size as per your requirements.

I hope this article was helpful. Thanks for reading it!

Categories
Apache mod-rewrite and htaccess related Articles

Redirect website visitors to maintenance page with htaccess

This article explains how to redirect website traffic to one single page named maintenance.php using RewriteRule in htaccess file .
With htaccess you can forward your all site traffic to one page when your site goes in maintenance mode or when you are working on your website.

Forward all site traffic to one page with htaccess

To redirect your site traffic to one page, you can use the code below in your htaccess file.

RewriteEngine on
#Redirect site traffic to maintenance.php
#Excude the destination file
RewriteCond %{REQUEST_URI} !/maintenance\.php [NC]
RewriteRule .* /maintenance.php [R,L]

This will redirect any calls to your website to /maintenance.php file. We excluded the destination file in our code because it could cause an infinite loop error if not excluded in RewriteCond .In simple words the condition above says “Do not redirect maintenance.php To itself

R in the code above represents a Temporary Redirect with 302 status code. You can not use a permanent redirect or R=301 here because using a permanent redirect status can conflict with your site SEO.

The following is the shorter version of the code above.

RewriteEngine On

RewriteRule !maintenance\.php /maintenance.php [R,L]

As you can see , we removed the RewriteCond and excluded the destination path in the pattern of the Rule.

The rules above do the trick but if you are using local css and js files on your maintenance.php page, then your page might look without style and script as the rule redirects all calls to one page including css and js. To fix this issue you will need to exclude css and js files from the rule. To exclude these files , you can use RewriteCond directive or you can also exclude them in the rule’s pattern .

RewriteEngine on
#Redirect site traffic to maintenance.php
#Excude the destination file
RewriteCond %{REQUEST_URI} !/maintenance\.php [NC]
#Excude the css files
RewriteCond %{REQUEST_URI} !\.css [NC]
#Excude the js files
RewriteCond %{REQUEST_URI} !\.js [NC]
RewriteRule .* /maintenance.php [R,L]

It’s that simple!
I hope this article was helpful to you. Thank you so much for your time.

Categories
Apache mod-rewrite and htaccess related Articles

How to redirect IP hostname address to domain with .htaccess?

This article explains how to redirect an IP hostname to domain name using a RewriteRule directive in htaccess file.
Every domain name on the internet has an unique IP address assigned to it that connects the domain to internet.
If your website is accessible with both an IP address like (192.66.77) and domain name (example.com) then, in order to maintain the SEO , its important that you either redirect your domain to ip address or the IP address to domain.

URL redirection from domain name to IP address is uncommon as nobody will want the IP address to appear on their browser address bar as it might be hard for your visitors to remember it. Domain name on the other hand is easier to remember and type.

Table of contents

IP address to domain name redirection with htaccess

With an .htaccess file you can easily redirect your domain ip address to domain name.
You can simply use a RewriteRule for this purpose.

The rule bellow 301 redirects domain IP address to domain name.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^192\.1.55.88$
RewriteRule (.*) https://domainname.com/$1 [L,R=301,NE]

Explainations :

The rule above will match any http_host string that looks something like 192.1.55.88 . In this example , it matches the http_host ip address and if the match if found then the RewriteRule line is read. The rule redirects the ip host domain to domainname .
In simple words the code above will redirect any request from 192.1.55.88/foobr to domainname.com/foobar .
The R=301 in the rule above represents a permanent redirection with 301 status code . The R=301 is important here as it tells search engines to update the results with a new domain.

A solution for Apache 2.4 users

If your Apache version is 2.4 or above you can also use Redirect with IF directive that’s supported on Apache 2.4 or higher.

The following should work on Apache 2.4* :

RewriteEngine On

 <If "%{HTTP_HOST} == '158.1.88.99'">
 RedirectMatch 301 ^/(.*)$ https://example.com/$1
 </If>

Related :

.htaccess tutorial for mod_rewrite beginners .