Archive for category Apache

Create Virtual Hosts and run multiple websites on Amazon server

You can create virtual hosts on any Linux or window platform if you are using Apache web server. Virtual hosts or v-hosts is basically Apache feature. I’m considering here Amazon server running with Apache version 2 or above. So, basically for creating v-hosts you need to modify Apache’s configuration (httpd.conf) file. You need the root access for all this modifications. Here I’m showing how we create the “name-based virtual hosting“. This is the easiest way of creating virtual host.
On Amazon server httpd.conf is located on ‘/etc/httpd/conf/httpd.conf‘ location. You need to follow these steps:-

1. First, take the backup of this file so in case if anything goes wrong you can rollback changes.
2. Download this file in your local machine and open it in any text editor.
3. Go to section 3 (Virtual Hosts) of this document.

### Section 3: Virtual Hosts

4. Un-comment this line

NameVirtualHost *:80

5. Now create v-hosts like this

<VirtualHost *:80>
    ServerAdmin admin@mydomain1.com
    DocumentRoot /var/www/html/domainname1
    ServerName www.domainname1.com
    ErrorLog logs/www.domainname1.com-error_log
    CustomLog logs/www.domainname1.com-access_log common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin admin@mydomain2.com
    DocumentRoot /var/www/html/domainname2
    ServerName www.domainname2.com
    ErrorLog logs/www.domainname2.com-error_log
    CustomLog logs/www.domainname2.com-access_log common
</VirtualHost>

Here we have create two virtual host one www.domainname1.com and second one www.domainname2.com. We have specified 5 Apache directive into a VirtualHost container. We can put almost any Apache directive into a VirtualHost container. Let’s discuss each one by one.

ServerAdmin – Here we specified email address of server administrator. If there is any issue Apache show this email address for contact.
DocumentRoot – Here we give the absolute path of directory where our website files are located.
ServerName – Here we give the name of our domain which is pointed to “/var/www/html/domainname1″ directory.
ErrorLog – Absolute path of Error log file for this domain.
CustomLog – Absolute path of custom log file for this domain.

6. Restart the Apache web server to apply these changes.
You can restart your Apache server using Terminal or Putty on Windows.

service httpd restart

Note:- One more thing which you need to do is you need to point both the domains “www.domainname1.com” and “www.domainname2.com” to the IP address of the Amazon server.

The Apache server it self find out the domain name form request header and point the incoming request to correct “DocumentRoot” directory.

, , ,

2 Comments


Create Search Engine Friendly(SEF) websites in PHP using URL rewriting

Search Engine Friendly URL

Search Engine Friendly URL

This article is for beginners who want to learn how to create search engine friendly(SEF) URLs (or Clean URLs) for a website. I am considering that you are using Apache web server. For clean URLs we need to do URL rewriting.

Note:- The “mod_rewrite” module should be enabled in Apache httpd.conf file.

We can define our rewrite conditions and rewrite rules in httpd.conf file but on shared hosting we don’t have access to this configuration file. So, In that case we can define these conditions and rules in ‘.htaccess’ file.

‘.htaccess’ file provide configuration settings on per-directory basic. You need to place this .htaccess file in your server’s root directory.

We need to put the following code in our .htaccess file.

<IfModule mod_rewrite.c>
  RewriteEngine on
 
  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

First, we are checking ‘mod_rewrite’ module is available and loaded by Apache web server. If this module is loaded then only our rewrite rule work.

Then we need to ‘ON’ RewriteEngine.

In rewrite conditions we are checking few things. Like in

RewriteCond %{REQUEST_FILENAME} !-f

we are checking the given file/path is not a file.

RewriteCond %{REQUEST_FILENAME} !-d

we are checking the given file/path is not a directory.

If the above two conditions satisfy than only our rewrite rule apply to the given URL. We are doing this because if browser request any resource like JavaScript or CSS or Image files than we don’t want to rewrite those URLs. If any path/URL which does not exist on our server will be rewrite by RewriteRule. All such type of requests will be handled by index.php file.

For example, If we write

http://www.websitename.com/article/25
(This URL is not exist on our server)

in address bar then this URL is rewritten as

http://www.websitename.com/index.php?q=article/25

Now, the only thing we need to do is to handle all the requests properly in our index.php file. We can create a file ‘url.inc’ for handling all the conditions and include it in top of our index.php file.

<?php
/*url.inc file*/
 
$url = @explode($_GET['q']);
 
if (isset($url[0]) && $url[0] == 'article' && isset($url[1]) && isnumeric($url[1])) {
	include('article.php'); 
	exit();	
}
else if (some other condition) {
	// include some other file
	exit();
}
?>

If any of the condition mentioned in the ‘url.inc’ file not match then index.php files content will execute.

<?php
/*index.php file*/
 
include('url.inc'); 
 
echo "This is the index.php file."
 
?>

, , ,

1 Comment


How redirect users to access your website with ‘www.’ prefix

Normally your website is accessible both with and without ‘www.’ prefix. And if you want your users to redirect to ‘www.’ prefix when they access your website without ‘www.’ prefix. You can do that using ‘.htaccess’ file.

We use .htaccess file to make configuration changes on per-directory basic. You need to place this .htaccess file in your server’s root directory.

Basically, we need to write some rewrite rules for our URL in this file.

<IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^yourwebsite.com$ [NC]
      RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [L,R=301]
</IfModule>

Reverse of this is also possible. If we want to redirect all website users to access the website without the ‘www.’ prefix. Then we need to write the following rewrite rules in out .htaccess file.

<IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^www.yourwebsite.com$ [NC]
      RewriteRule ^(.*)$ http://yourwebsite.com/$1 [L,R=301]
</IfModule>

Note:- The “mod_rewrite” module should be enabled in Apache httpd.conf file. Otherwise URL rewriting will not work. For enabling this module you need to remove ‘#’ before

LoadModule rewrite_module modules/mod_rewrite.so

line in your httpd.conf file. We are also checking in our .htaccess file that the mod_rewrite module is enabled or not. You should also restart your Apache server to apply these changes.

, ,

1 Comment