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.


Technorati tags:



Comments /foo-web/htaccess-redirects.html




Add your comments

ad

Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner


Views for this page
Today This Week This Month This Year  Overall
457481,273 3,708

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. We appreciate comments and article submissions.

Publishing your articles here

pavatar.jpg
More:
       - Web/HTML
       - Blogging




Unix/Linux Consultants

Your ad here - $24.00 yearly!

http://www.breakthru.com.au SCO (Openserver and Unixware), Unix, Solaris and Linux Consulting services including: Secure Networking Solutions; Linux based Firewalls; Backup Solutions; Secure Home to Office Network Setup; Phone, Remote and On-Site Support available - Satisfaction Guaranteed!


larryi@ccamedical.com SCO OS5, Debian Linux, RedHat Linux, MySQL, Apache, AJAX development using dXport/dL4/Unibasic, Windows Connectivity, Sharing Resouces, Automation, Shell Scripting


UBB Computer Services Support for Openserver, Unixware and Linux. Windows integration with Unix/Linux servers. Hardware, Backup and Networking issues. Located near Sacramento CA, we provide onsite support throughout Northern CA and Nationwide via remote access. We are a SCO Authorized Partner and a Microlite BackupEdge Certified Reseller.




Twitter
  • Jun 26 21:28
    Lost 5 cents at poker tonight. Hard to do with 10 and 20 cent betting..
  • Jun 23 07:01
    Hypermiling:









Change Congress

Related Posts