.htaccess stuff

From brokenpoet.org wiki

Contents

AddHandler

For running, php4 and php5 concurrently, who wants to have to use the .php5 extension???

So:

 AddHandler application/x-httpd-php5 .php

Put this .htaccess entry in a subdirectory even if the main page is php4!

html files processed as php with AddHandler

This is a simple line to include in an .htaccess file:

AddHandler application/x-httpd-php5 .html .htm .php .phtml

Disable Directory Browsing

Options All -Indexes

Password Protection removed in a Sub Directory

If you have a directory that is password protected and you need a subdirectory of that accessible without a password, you will need to create and .htaccess file in the subdirectory with contains commands to override the Auth info in the parent directory.

That information is:

AuthType None
Satisfy any

Redirect Directory call to Root

This is an odd one I came across. Say you want:

www.domain.com/directory/1.php www.domain.com/directory/2.html www.domain.com/directory/3.asp

to all redirect to

www.domain.com

Syntax that would work (although it reads odd to me):

RedirectMatch Permanent /directory/*  domain.com 

Rewrite for HTTPS

To redirect all traffic to https:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://domain.com [R=301,L]

Rewrite domain.com/index.php to domain.com/

Some programs, such as WordPress, show the same duplicate content for http://www.domain.com/index.php and http://www.domain.com/. If you’ve read about using a robots.txt file for WordPress SEO, than you already understand this setup results in Duplicate Content penalties being levied against your Blog and Web Site by Search Engines.

The Redirect Fix

The fix is a bit of clever .htaccess code utilizing mod_rewrite to only redirect index.php if the request for /index.php came from a client (e.g. browser or web robot), and not if the request is an internal redirect that apache does when / is requested so that it can serve the contents of index.php.

.htaccess mod_rewrite code

domain.com/index.php:

Options +FollowSymLinks
DirectoryIndex index.php
 
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L] 
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

domain.com/blog/index.php:

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
RewriteBase /blog/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /blog/index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/blog/ [R=301,L] 
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

Rewrite Query String

I've only had to do this a few times but lets say you want rewrite:

/page.html?p=end

to

/index.html 

There is a query_string directive for this:

Options +FollowSymlinks
RewriteEngine on

rewriteCond %{query_string} p=end
rewriteRule ^page.html$ /index.html? [R,L]

Rewrite Non-WWW to WWW

RewriteEngine on

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

OR to redirect all www and non-www to port 443:

RewriteCond %{SERVER_PORT} ^80$ 
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^/?$ "https\:\/\/domain\.com" [R=301,L]
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^/?$ "https\:\/\/domain\.com" [R=301,L]

Rewrite site without subdirectory

Will take all traffic to a new site except for the specific references to the subdirectory.

ex: http://domain.com needs to be redirected to http://newdomain.com - however http://domain.com/subdir_to_leave_out/ needs to still resolve to the original domain (and not be redirected).

RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteCond %{HTTP_HOST} !^/subdir_to_leave_out/
RewriteRule ^/?$ http://www.newdomain.com [R=301,L]

The and condition is implied when listing RewriteCond in this manner.

Alternate possibility related to Drupal but applicable elsewhere:

RewriteCond %{THE_REQUEST} !/subdir_to_leave_out/.*  
  
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Personal tools