Apache

From Marketing Wiki

Jump to: navigation, search

Contents

Installing Apache

  • Apache2triad - Install Apache, PHP, MySQL and more on Windows with just a few clicks.

mod_rewrite and .htaccess

.htaccess Recipes

Prevent Directory Listings

Options All -Indexes

Block IP Addresses

Replace nn.nnn.nnn.nnn with the actual IP addresses that you want to ban:

order allow,deny 
deny from nn.nn.nn.nnn
deny from nn.nn.nnn.nn
deny from nn.nnn.nnn.nnn
allow from all

Remove Trailing Slashes

RewriteRule ^(.*[^/])/$ /$1 [R=301,L]

301 Redirect

This will 301 redirect one page to another:

RewriteRule ^the-old-page$ http://example.com/the-new-page [R=301,L]

Redirect Query Strings

If the URL has a query string it needs a different kind of redirect rule:

RewriteCond %{QUERY_STRING} p=the-query-string
RewriteRule ^index\.php$ /the-new-pages.html? [R=301,L]

The above rule would redirect example.com/index.php?p=the-query-string to example.com/the-new-page.html.

Canonical URLs

The following rule redirect from the non-www version of a domain name to the www version, e.g., from example.com to www.example.com:

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

This variation does the same, but says "if it's anything but www then redirect to the www":

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

This redirect from the www to the non-www:

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