Skip to main content

Basic htaccess

The .htaccess file tells the web servers how to act when your website’s needs differ from the default behavior.

Some examples include:

Redirect all traffic to a new domain / URL / etc.

RewriteEngine on
RewriteRule ^/?(.*)$ %{REQUEST_SCHEME}://newdomain.osu.edu/optional/$1 [L,NE,QSA,R=301]

Forward all traffic to a sub-directory of your normal Document Root (htdocs) :

RewriteBase /
RewriteRule ^$ MySubDir/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ MySubDir/$1 [L]

Prevent a web application’s catch-all script from intercepting and breaking Shibboleth logins:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/Shibboleth.sso($|/)
RewriteRule . - [L]

Re-enable Apache’s directory listing feature:

Options +Indexes

Redirect any request for a non-existent page an alternate catch-all location:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /pagenotfound.php [L,R=301,QSA,NE]

Block access to files in a directory if they’re being called directly or from a site other than yours: (useful for js, css, or image directories)

SetEnvIfNoCase Referer "mysite\.osu\.edu" ok_ref
<Files "*">
Order deny,allow
Deny from all
Allow from env=ok_ref
</Files>

Disable all browser-side caching:

Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"

Redirect traffic to new locations based on the Domain & Subfolder info in the URL; 
    In This case, Subdirectory #1  goes to one location, Subdirectory #2 to another, and all else goes to your u.osu site:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.osu\.edu [NC]
     RewriteRule ^subdir1/? https://another.domain.org/ [R=301,NC,L]
     RewriteRule ^subdir2/? https://www.yetanotherdomain.net/ [R=301,NC,L]
     RewriteRule ^(.*)$ https://u.osu.edu/mypage/ [R=301,NC,L]