301 Redirect

August 12th, 2009 Comments off

IIS Redirect

  • In internet services manager, right click on the file or folder you wish to redirect
  • Select the radio titled "a redirection to a URL".
  • Enter the redirection page
  • Check "The exact url entered above" and the "A permanent redirection for this resource"
  • Click on ‘Apply’

PHP Redirect

<? Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" ); ?>

ASP Redirect

<%@ Language=VBScript %>
<%
 Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.new-url.com/"
%>

ASP .NET Redirect

 <script runat="server">
 private void Page_Load(object sender, System.EventArgs e)
  {  Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
  }
</script>

Redirect Old domain to New domain – .htaccess

Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain. The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

#BOF :: Script for URL Rewrite
Options +FollowSymLinks
RewriteEngine on
rewritecond %{http_host} ^olddomain.com [NC]
rewriterule ^(.*)$ http://www.xyz.com/$1 [r=301,L]
#EOF :: Script for URL Rewrite

Please REPLACE www.newdomain.com in the above code with your actual domain name. In addition to the redirect I would suggest that you contact every backlinking site to modify their backlink to point to your new website. Note - This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Redirect to www – .htaccess

Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

Please REPLACE domain.com and www.newdomain.com with your actual domain name. Note –  This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Archiving Files at the Shell Prompt

August 11th, 2009 1 comment

A tar file is a collection of several files and/or directories in one file. This is a good way to create backups and archives. Some of the options used with the tar are:

  • -c — create a new archive.
  • -f — when used with the -c option, use the filename specified for the creation of the tar file; when used with the -x option, unarchive the specified file.
  • -t — show the list of files in the tar file.
  • -v — show the progress of the files being archived.
  • -x — extract files from an archive.
  • -z — compress the tar file with gzip.
  • -j — compress the tar file with bzip2.

To create a tar file, type:

tar -cvf filename.tar directory/file

In this example, filename.tar represents the file you are creating and directory/file represents the directory and file you want to put in the archived file. You can tar multiple files and directories at the same time by listing them with a space between each one:

  tar -cvf filename.tar /home/mine/work /home/mine/school

The above command places all the files in the work and the school subdirectories of /home/mine in a new file called filename.tar in the current directory. To list the contents of a tar file, type:

 tar -tvf filename.tar

To extract the contents of a tar file, type:

 tar -xvf filename.tar

This command does not remove the tar file, but it places copies of its unarchived contents in the current working directory, preserving any directory structure that the archive file used. For example, if the tarfile contains a file called bar.txt within a directory called foo/, then extracting the archive file will result in the creation of the directory foo/ in your current working directory with the file bar.txt inside of it. Remember, the tar command does not compress the files by default. To create a tarred and bzipped compressed file, use the -j option:

  • tar -cjvf filename.tbz file
  • tar -cjvf filename.tbz foldername/

tar files compressed with bzip2 are conventionally given the extension .tbz; however, sometimes users archive their files using the tar.bz2 extension. The above command creates an archive file and then compresses it as the file filename.tbz. If you uncompress the filename.tbz file with the bunzip2 command, the filename.tbz file is removed and replaced with filename.tar. You can also expand and unarchive a bzip tar file in one command:

 tar -xjvf filename.tbz

To create a tarred and gzipped compressed file, use the -z option:

 tar -czvf filename.tgz file

tar files compressed with gzip are conventionally given the extension .tgz. This command creates the archive file filename.tar and then compresses it as the file filename.tgz. (The file filename.tar is not saved.) If you uncompress the filename.tgz file with the gunzip command, the filename.tgz file is removed and replaced with filename.tar. You can expand a gzip tar file in one command:

tar -xzvf filename.tgz

Compressing Files at the Shell Prompt

August 11th, 2009 Comments off

Compressed files use less disk space and download faster than large, uncompressed files. In Red Hat Linux you can compress files with the compression tools gzip, bzip2, or zip.

The bzip2 compression tool is recommended because it provides the most compression and is found on most UNIX-like operating systems. The gzip compression tool can also be found on most UNIX-like operating systems. If you need to transfer files between Linux and other operating system such as MS Windows, you should use zip because it is more compatible with the compression utilities on Windows.

Compression Tool File Extension Uncompression Tool
gzip .gz gunzip
bzip2 .bz2 bunzip2
zip .zip unzip

By convention, files compressed with gzip are given the extension .gz, files compressed with bzip2 are given the extension .bz2, and files compressed with zip are given the extension .zip. Files compressed with gzip are uncompressed with gunzip, files compressed with bzip2 are uncompressed with bunzip2, and files compressed with zip are uncompressed with unzip.

Bzip2 and Bunzip2

To use bzip2 to compress a file,The file will be compressed and saved as filename.bz2.  type the following command at a shell prompt:

bzip2 filename

To expand the compressed file , The filename.bz2 is deleted and replaced with filename.type the following command:

bunzip2 filename.bz2

You can use bzip2 to compress multiple files and directories at the same time by listing them with a space between each one:

bzip2 filename.bz2 file1 file2 file3 /usr/work/school

The above command compresses file1, file2, file3, and the contents of the /usr/work/school directory (assuming this directory exists) and places them in a file named filename.bz2.

Gzip and Gunzip

To use gzip to compress a file, type the following command at a shell prompt:

gzip filename

The file will be compressed and saved as filename.gz. To expand the compressed file, type the following command:

 gunzip filename.gz

The filename.gz is deleted and replaced with filename. You can use gzip to compress multiple files and directories at the same time by listing them with a space between each one:

gzip -r filename.gz file1 file2 file3 /usr/work/school

The above command compresses file1, file2, file3, and the contents of the /usr/work/school directory (assuming this directory exists) and places them in a file named filename.gz.

Zip and Unzip

To compress a file with zip, type the following command:

 zip -r filename.zip

filesdir In this example, filename.zip represents the file you are creating and filesdir represents the directory you want to put in the new zip file. The -r option specifies that you want to include all files contained in the filesdir directory recursively. To extract the contents of a zip file, type the following command:

unzip filename.zip

You can use zip to compress multiple files and directories at the same time by listing them with a space between each one:

 zip -r filename.zip file1 file2 file3 /usr/work/school

The above command compresses file1, file2, file3, and the contents of the /usr/work/school directory (assuming this directory exists) and places them in a file named filename.zip.

How to install a Network card in linux

July 30th, 2009 Comments off

The Easy way

RedHat/Fedora distributions of linux ships with Kudzu a device detection program which runs during systems initialization (/etc/rc.d/init.d/kudzu). This can detect a newly installed NIC and load the appropriate driver. Then use the program /usr/sbin/netconfig to configure the IP address and network settings. The configuration will be stored so that it will be utilized upon system boot.

The Manual method

 Run the command

# update-pciids

By using  lshal , lsmod and lspci we get the detail property of the hardware. To find the name of the network adapter Enter:

*Note : we have to install gcc and kernel-devel pakage in RHEL 5

 lspci -v | grep Ethernet -A 1
  1. Move the base driver tar file to the directory of your choice. For example, use ‘/home/username/e1000′ or ‘/usr/local/src/e1000′.
  2.  Untar/unzip the archive, where <x.x.x> is the version number for the driver tar file: tar zxf e1000-<x.x.x>.tar.gz
  3.  Change to the driver src directory, where <x.x.x> is the version number for the driver tar: cd e1000-<x.x.x>/src/ 
  4.  Compile the driver module: make install The binary will be installed as: /lib/modules/<KERNEL VERSION>/kernel/drivers/net/e1000/e1000.[k]o The install location listed above is the default location. This may differ for various Linux distributions. 
  5.  Load the module using either the insmod or modprobe command: modprobe e1000 insmod e1000 Note that for 2.6 kernels the insmod command can be used if the full path to the driver module is specified. For example: insmod /lib/modules/<KERNEL VERSION>/kernel/drivers/net/e1000/e1000.ko With 2.6 based kernels also make sure that older e1000 drivers are removed from the kernel, before loading the new module:rmmod e1000; modprobe e1000
  6.  Assign an IP address to the interface by entering the following, where <x> is the interface number: ifconfig eth<x> <IP_address>
  7. Verify that the interface works. Enter the following, where <IP_address> is the IP address for another machine on the same subnet as the interface that is being tested: ping <IP_address>
  8. To find the driver version: Type  ethtool -i <ethx> where <ethx> is the Ethernet port . Reads the driver name and version .

Example for the first Ethernet port, eth0

# ethtool -i eth0

How To Mount USB cdrom In Linux

July 30th, 2009 Comments off

Verify with the lsmod command that the USB modules and SCSI modules are loading.A USB cdrom drive will have a device ID of /dev/scd0 assuming no other SCSI CDROM.

#mkdir /mnt/usb-cdrom
# mount /dev/scd0 /mnt/usb-cdrom

How to Back Up and Restore a MySQL Database

July 23rd, 2009 1 comment

If you’re storing anything in MySQL databases that you do not want to lose, it is very important to make regular backups of your data to protect it from loss. This tutorial will show you two easy ways to backup and restore the data in your MySQL database. You can also use this process to move your data to a new web server.

Back up From the Command Line (using mysqldump)

If you have shell or telnet access to your web server, you can backup your MySQL data by using the mysqldump command. This command connects to the MySQL server and creates an SQL dump file. The dump file contains the SQL statements necessary to re-create the database. Here is the proper syntax:

$ mysqldump –opt -u [uname] -p[pass] [dbname] > [backupfile.sql]
  • [uname] Your database username
  • [pass] The password for your database (note there is no space between -p and the password)
  • [dbname] The name of your database
  • [backupfile.sql] The filename for your database backup
  • [--opt] The mysqldump option

For example, to backup a database named ‘Tutorials’ with the username ‘root’ and with no password to a file tut_backup.sql, you should accomplish this command:

$ mysqldump -u root -p Tutorials > tut_backup.sql

This command will backup the ‘Tutorials’ database into a file called tut_backup.sql which will contain all the SQL statements needed to re-create the database.

With mysqldump command you can specify certain tables of your database you want to backup. For example, to back up only php_tutorials and asp_tutorials tables from the ‘Tutorials’ database accomplish the command below. Each table name has to be separated by space.

$ mysqldump -u root -p Tutorials php_tutorials asp_tutorials > tut_backup.sql

Sometimes it is necessary to back up more that one database at once. In this case you can use the –database option followed by the list of databases you would like to backup. Each database name has to be separated by space.

$ mysqldump -u root -p –databases Tutorials Articles Comments > content_backup.sql

If you want to back up all the databases in the server at one time you should use the –all-databases option. It tells MySQL to dump all the databases it has in storage.

$ mysqldump -u root -p –all-databases > alldb_backup.sql

The mysqldump command has also some other useful options:

–add-drop-table: Tells MySQL to add a DROP TABLE statement before each CREATE TABLE in the dump.

–no-data: Dumps only the database structure, not the contents.

–add-locks: Adds the LOCK TABLES and UNLOCK TABLES statements you can see in the dump file.

The mysqldump command has advantages and disadvantages. The advantages of using mysqldump are that it is simple to use and it takes care of table locking issues for you. The disadvantage is that the command locks tables. If the size of your tables is very big mysqldump can lock out users for a long period of time.

Back up your MySQL Database with Compress

If your mysql database is very big, you might want to compress the output of mysqldump. Just use the mysql backup command below and pipe the output to gzip, then you will get the output as gzip file.

$ mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]

If you want to extract the .gz file, use the command below:

$ gunzip [backupfile.sql.gz]

Restoring your MySQL Database

Above we backup the Tutorials database into tut_backup.sql file. To re-create the Tutorials database you should follow two steps:

  • Create an appropriately named database on the target machine
  • Load the file using the mysql command:
$ mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]

Have a look how you can restore your tut_backup.sql file to the Tutorials database.

$ mysql -u root -p Tutorials < tut_backup.sql

To restore compressed backup files you can do the following:

gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]

If you need to restore a database that already exists, you’ll need to use mysqlimport command. The syntax for mysqlimport is as follows:

mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

Enable htaccess on Apache

July 16th, 2009 Comments off

.htaccess files allows us to change configurations on our servers per directory or subdirectory. we may enable htaccess files by editing our httpd.conf rewmoving the comment on line from

#LoadModule rewrite_module modules/mod_rewrite.so

to

LoadModule rewrite_module modules/mod_rewrite.so

we need to change the AllowOverride directive also from

<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Satisfy all
</Directory>

to

<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>

You can also rename your .htaccess file by adding the line below on you httpd.conf file

AccessFileName [filename]

example: AccessFileName .configuration

Allow, Deny, Disable, Enable Directory Listing in .htaccess

May 26th, 2009 Comments off

When a web browser is pointed to a directory on your web site which does not have an index.html file (or any other index file) in it, the files in that directory can be listed on a web page. Let us see few snippets that can be added in htaccess file to allow or avoid directory listing in apache server.

Enable / Disable directory Listing

To allow a web server to produce a directory listing, whenever you point a directory without index file. Add following line in your .htaccess file.

# Options +Indexes
# # or #

# IndexIgnore *

To disable or prevent the directory access add following line in your .htaccess file. If user points the browsers to a directory which does not have index file then in this case 403 error will be

Options -Indexes

Using above option will display the forbidden error page  when we try to access any directory without index file.

Change Listing style

You may want to display other details while showing the directory listing. This includes file icons, file size, modification date and more. This can be done by adding fancy style to your htaccess file. Add following snippet in .htaccess file.

IndexOptions +FancyIndexing

To remove the fancy directory listing or to display normal directory listing, use -FancyIndex

IndexOptions -FancyIndexing

Ignore files with specific extension

It may happen that you may need to ignore certain files to get displayed in directory listing. This can be achieved using IndexIgnore directive in .htaccess file. Following snippet will not display .zip and .txt file in directory listing.

IndexIgnore *.zip *.txt

Modify Index File

t is possible to change the default index file from index.html (index.php, index.jsp …) to any other file. Following line will change the index file to Home.html.

DirectoryIndex Home.html

How to: Reset MySQL root password

May 26th, 2009 Comments off

Resetting the root password of a MySQL database is trivial if you know the current password if you don’t it is a little trickier. Thankfully it isn’t too difficult to fix, and here we’ll show one possible way of doing so.

If you’ve got access to the root account already, because you know the password, you can change it easily:

abc@xyz:~$ mysql --user=root --pass mysql
Enter password:
mysql> update user set Password=PASSWORD('new-password-here') WHERE User='root';
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2  Changed: 2  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)  

mysql> exit
Bye

Forget the MySQL root password?

However if you don’t know the current password this approach will not work – you need to login to run any commands and without the password you’ll not be able to login!Thankfully there is a simple solution to this problem also, we just need to start MySQL with a flag to tell it to ignore any username/password restrictions which might be in place. Once that is done you can successfully update the stored details.

First of all you will need to ensure that your database is stopped:

root@xyz:~# /etc/init.d/mysql stop
Now you should start up the database in the background, via the mysqld_safe command:  

root@gaurav:~# /usr/bin/mysqld_safe --skip-grant-tables &
[1] 4271
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6763]: started

Here you can see the new job (number “1″) has started and the server is running with the process ID (PID) of 4271.

Now that the server is running with the -skip-grant-tables flag you can connect to it without a password and complete the job:

root@xyz:~$ mysql --user=root mysql
Enter password:  

mysql> update user set Password=PASSWORD('new-password-here') WHERE User='root';
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2  Changed: 2  Warnings: 0  

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
mysql> exit
Bye

Now that you’ve done that you just need to stop the server, so that you can go back to running a secure MySQL server with password restrictions in place. First of all bring the server you started into the foreground by typing “fg”, then kill it by pressing “Ctrl+C” afterward.

This will now allow you to start the server:

# root@xyz:~# /etc/init.d/mysql start
# Starting MySQL database server: mysqld.
# Checking for corrupt, not cleanly closed and upgrade needing tables..

Now everything should be done and you should have regained access to your MySQL database(s); you should verify this by connecting with your new password:

root@xyz:~# mysql --user=root --pass=new-password-here
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 5.0.24a-Debian_4-log  

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.  

mysql> exit
Bye

MySQL Database Backup using mysqldump command

May 26th, 2009 Comments off

MySQL provide a great command line utility to take backup of your MySQL database and restore it. mysqldump command line utility is available with MySQL installation (bin directory) that can be used to achieve this.

Getting backup of a MySQL database using mysqldump

  mysqldump --user [user name] --password=[password] [database name] > [dump file]
                             or
  mysqldump -u[user name] -p[password] [database name] > [dump file]

Backup multiple databases in MySQL

  mysqldump -u[user name] -p[password] [database name 1] [database name 2] .. > [dump file]

Backup all databases in MySQLrint?

  shell> mysqldump -u[user name] -p[password] -all-databases > [dump file]

Backup a specific table in MySQL

shell> mysqldump --user [username] --password=[password] [database name] [table name] /
> /tmp/sugarcrm_accounts_contacts.sql  

Restoring MySQL database

The mysqldump utility is used only to take the MySQL dump. To restore the database from the dump file that you created in previous step, use mysql command.

shell> mysql --u [username] --password=[password] [database name] < [dump file]