Sites

Wikipedia
SlashDot
Gentoo Linux
Loop-AES Encryption
Geoiptool
Showing posts with label HTTPD. Show all posts
Showing posts with label HTTPD. Show all posts

Wednesday, July 1, 2009

Mod Rewrite Explained...

In this tutorial, I'm going to be teaching what mod rewrite is and few examples of its uses. Mod rewrite is a powerful tool and one of the simplest ways to make your website more SEO friendly.

So lets get started...

What is Mod Rewrite?

Mod rewrite is a part of apache servers that can rewrite requested URLs on the fly. As it supports an endless number of rules that in turn have unlimited attached rule conditions it is very flexible and an important URL manipulation mechanism. It can be used for internet users and for search engine friendly URLs. This increases the chance of the database driven website to be indexed. Which is a very good thing for you and your website.


Enabling / Installing Mod Rewrite

The first thing we want to do is make sure that we have mod_rewrite on our server. By default, Apache has mod_rewrite installed, but not enabled. The easiest way to test if mod_rewrite is enabled on your server is to create a .htaccess file in a test directory such as /modrewrite-test/.htaccess and edit it to look like this:



Options +FollowSymLinks
RewriteEngine On

Now to test, attempt to browse to the subdirectory(test). One of two things could happen:
  1. No errors mod_rewrite engine is now enabled.
  2. 500, Internal Server Error If you get this message then mod_rewrite was not installed or enabled on your server
If you found that mod_rewrite was not installed on your server, follow these steps.
  1. Find the httpd.conf file (usually you will find it in a folder called conf, config or something along those lines)
  2. Inside the httpd.conf file uncomment the line LoadModule rewrite_module modules/mod_rewrite.so (remove the pound '#' sign from in front of the line)
  3. Also find the line ClearModuleList is uncommented then find and make sure that the line AddModule mod_rewrite.c is not commented out.
After these steps, restart your apache server:

/etc/init.d/httpd restart
now mod_rewrite should be enabled. Be sure to test mod_rewrite again once apache server has been restarted.

Using Mod Rewrite To Always Remove / And Add WWW

One easily fixed problem that many websites have is duplicate results in search engines. In any given search engine, http://www.yoursite.com/ and http://yoursite.com are two completely different websites. This is a big problem because the content in your website is identified as duplicate content, and that brings your search engine rankings down. Using mod_rewrite, we can fix this issue.

At the top of any .htaccess file, you need to turn Mod Rewrite on. This should always be at the top of your .htaccess file.(You only need to turn Mod Rewrite on once)
Also you need to set your Rewrite Base. This is normally the directory in which the .htaccess file is located. In this example the .htaccess is located in the root directory:


RewriteEngine On
RewriteBase /

To always add the www to the front of your site, add the following to your .htaccess file located in the root dir(/):

RewriteCond %{HTTP_HOST} ^yoursite.com$
RewriteRule (.*) http://www.yoursite.com$1 [R=301]


Bookmark and Share

Saturday, June 20, 2009

Howto Deny Countries With Apache & .htaccess

The following script is using blogama.org IP geolocation API to automatically generate Apache .htaccess file to deny or allow specific countries. You can put this script under crontab and the .htaccess rules will be automatically updated. Also, it can update multiple .htaccess files.

Country codes
You need to know the ISO country code you want to deny/allow. The list is available here.

Method without the automated script

Go to:

http://blogama.org/country_query.php?country=CA,US&output=htaccess_deny

Where country is the list or countries, with a comma between them and output is either htaccess_deny or htaccess_allow.

Before You Start

Create a text file named htaccessfile.txt (in the WORKDIR of the script, see below). In that file, put all (existing!) .htaccess files you wish to update. For example:

/var/www/example.com/.htaccess
/var/www/mydomain.com/.htaccess

Script configuration

Edit the Top Section of the Script:
###MODIFY THIS SECTION###
WORKDIR="/root/"
HTACCESSFILE="htaccessfile.txt"
HTACCESSBLOCK="htaccess-blocklist.txt"
TEMPFILE="htaccess.temp"
COUNTRIES="US,CA"
TYPE="allow"
#########################



WORKDIR: is a writable directory where the script will be located.
HTACCESSFILE: is the file where you will put your .htaccess paths.
HTACCESSBLOCK and TEMPFILE: are temporary file that will be deleted at the end of the script execution.
COUNTRIES: is the list of countries you wish to deny/allow, separated with a comma.
TYPE: "allow" or "deny" access to these countries.

#########################
THE SCRIPT
!/bin/bash
###BLOGAMA.ORG###
###MODIFY THIS SECTION###
WORKDIR="/root/"
HTACCESSFILE="htaccessfile.txt"
HTACCESSBLOCK="htaccess-blocklist.txt"
TEMPFILE="htaccess.temp"
COUNTRIES="US,CA"
TYPE="deny"
#########################
#####DO NOT MAKE MODIFICATIONS BELOW#####
cd $WORKDIR
#Get the file from blogama.org API
wget -c --output-document=$HTACCESSBLOCK "http://blogama.org/country_query.php?country=$COUNTRIES&output=htaccess_$TYPE"
for i in $( cat $WORKDIR$HTACCESSFILE ); do
if [ -f $i ]; then
cat $i 2>&1 | grep "COUNTRY_BLOCK_START"
if [ "$?" -ne "1" ]; then #ALREADY IN HTACCESS
sed '/#COUNTRY_BLOCK_START/,/#COUNTRY_BLOCK_END/d' $i > $WORKDIR$TEMPFILE
cat $WORKDIR$HTACCESSBLOCK >> $WORKDIR$TEMPFILE
mv $WORKDIR$TEMPFILE $i
else #NOT IN HTACCESS
cat $WORKDIR$HTACCESSBLOCK >> $i
fi
fi
done
rm -f $WORKDIR$HTACCESSBLOCK

Make sure you make the script executable:
chmod +x whatever_you_called_this_script

Now you can deny access to non-relevant countrys that do not suit
your sites purpose. Or may just be hogging bandwidth for no
reason.

-Enjoy



Bookmark and Share