APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

using .htaccess files to redirect pages

Every directory in a Apache website can have an optional .htaccess file. This can be used to require passwords to access the files therein or to redirect requests to other pages. Everything you can do in an .htaccess file can also be done in your web server's configuration file, but that can be a little intimidating and confusing for new web masters. The .htaccess is slightly less efficient, but easy to use.

Using .htaccess to redirect requests and to help with security.

Sometimes I make a mistake in a link I have put in a newsgroup posting or have sent by email. Other times I might decide that my file name is badly chosen or potentially confusing. An entry in .htaccess can fix that instantly. Let's say, for example, that I originally created /serv.html but now want to rename it /services.html. If I just renamed it, any external links referencing that file will get 404 errors - File Not Found. I could handle that with a custom 404 page script, but it's easier with .htaccess:

 RedirectMatch permanent ^/serv.html /services.html
 
 

If you understand regular expressions, you see this works much like sed, vi or Perl substitutions: /serv.html is translated to /services.html. The substitutions can be much more general:

 RedirectMatch permanent ^/[uU]nixarticles.html$  /Unixart/index.html
 
 

and can carry forward information from the matched pattern:

 RedirectMatch permanent  (.*).shtm$  $1.html
 
 

Redirection can also help with nasty attempts at breaking in. These are all Microsoft exploits redirected to an address that will normally time out:

 RedirectMatch permanent .*cmd.exe.*$  http://192.0.2.1 
 RedirectMatch permanent .*rush.echo.*$  http://192.0.2.1 
 RedirectMatch permanent .*root.exe.*$  http://192.0.2.1 
 RedirectMatch permanent .*\/_vti_bin\/.*$  http://192.0.2.1 
 RedirectMatch permanent .*\/scripts\/\.\..*$  http://192.0.2.1 
 RedirectMatch permanent .*\/_mem_bin\/.*$  http://192.0.2.1 
 RedirectMatch permanent .*\/msadc\/.*$  http://192.0.2.1 
 RedirectMatch permanent .*\/MSADC\/.*$  http://192.0.2.1 
 RedirectMatch permanent .*\/c\/winnt\/.*$  http://192.0.2.1 
 RedirectMatch permanent .*\/d\/winnt\/.*$  http://192.0.2.1 
 RedirectMatch permanent .*\/x90\/.*$  http://192.0.2.1 
 RedirectMatch permanent .*abcdef.*$  http://192.0.2.1 
 
 

Be careful with these though: it's not hard to accidentally match a legitimate page on your site if you get too aggressive with wild cards.

All of this comes under the general umbrella of the Apache Rewrite module; you can find much more on the web by searching for that.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> using .htaccess files to redirect pages




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





Basic happened to be on a GE timesharing system that was done by Dartmouth, and when GE decided to franchise that, it started spreading Basic around just because it was there, not because it had any intrinsic merits whatsoever. (Alan Kay)

What worries me about religion is that it teaches people to be satisfied with not understanding. (Richard Dawkins)












This post tagged: