About Blog Login

First Steps

So, I settled on a plan from Hostinger. They have great Shared web hosting, fast servers, and I tried their support and it was very good. The plans are also very affordable. They have WordPress and many other frameworks installers. But I needed just plain old PHP and they have that too. A great thing is that you can also use a Git repository (can be private) to push the code to it. Very handy.

Ok, so with that out of the way, we have a good starting point. What we will need to do next is to setup a development environment.

I had good experience with both a full fledged Linux PC or a VM, and you can also do the same using WSL on a Windows Machine. You could also use Something like XMPP if you want a fully Windows system without any Linux

I will be using Linux and/or WSL, which are mostly identical for our purposes.

Setting up the Web-server

Install your web-server of choice. I have opted to use the "standard" Apache, which is available everywhere, and is used by Production server (Hostinger).

Now we can do some tests. I'm assuming your development files are in /var/www/html. You can change this in your Apache config too, but I kept it there, and relaxed the security on that dev server so I don't need to sudo every time I need to save a file.

Now, in /var/www/html create a test page t1.php:

echo "Hello, World!";

on your dev machine, go to http://localhost/t1.php. and hopefully you will see the Hello World page.

If you are using a WSL machine, then you need to find the ip address so you can navigate to the page from Windows Browser. If you do ip addr on your WSL, you should see an address that usually starts with 172.x.x.x. This is the address to use in Windows to test your pages.

Routing and rewrite rules

By default, Apache serves the files as they are found in the virtual directory. So localhost/t1.php will server the file /var/www/html/t1.php. There are ways to configure Apache to drop the php extension. so you can server the same /var/www/html/t1.php by navigating to localhost/t1 And you can also drop the .html in the URL.

But this NOT what we want. We want to have a single entry file (index.php) instead of finding an actual file at that URI. Doing it this way, we can have nice URi like localhost/view/1/the-first-blog, without a file by that name existing. This can easily be done by a .htaccess file.

Our .htaccess file will look like the following:

<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteRule ^index\.php$ - [L]
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteRule . index.php [L,QSA]

The important part is this line: RewriteRule . index.php [L,QSA] which routes everything to index.php. This will be the entry point to our application, regardless of the URI.

On most Linux installations adding .htaccess file will have no effect. You also need to update the Apache configuration and change the AllowOverride to All as below:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

For security, the default value of None will ignore .htaccess. There are other way of doing it, but IMHO, .htaccess is easiest and is what most Web Hosts will give you anyway, so better use the same config for development too.

Next post, we'll see how to do the routing in our index.php file.

Add a Comment