Smartlab Software logo

htaccess Tips

Helpful htaccess tips for an Apache server.

301 Redirect

In order not to lose valuable rankings with a web page that has been indexed it needs to be redirected if it is ever deleted or the filename is changed.

redirect 301 /old-web-page.htm http://www.mydomain.com/new-web-page.htm

If your web path has spaces in it, use double quotes:

redirect 301 "/abc/web page.html" http://www.mydomain.com/new-web-page.htm

To test out the redirect enter in the old web page URL in your browser. It should redirect to the new web page.

Referrer Spam

Redirects referrer spam to a forbidden page or a website of your choice..

# Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(www\.)?spammersite1.com.*$ [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?spammersite2.com.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?spammersite3.com.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?spammersite7.com.*$ [NC]
RewriteRule .* - [F,L]
The last line can be replaced by:
RewriteRule \.*$ http://www.some-other-website.com [R,L]
which will redirect a referrer spammer to another website.
If you wish to block access based on an IP address use this:
# Deny access based on the IP address or host name of the offending site
 <Directory /usr/local/etc/httpd/htdocs>
   Deny from 12.14.111.333
   Deny from bad-site.com
 </Directory>

Related resources