276 lines
11 KiB
HTML
276 lines
11 KiB
HTML
<!--startcut ==============================================-->
|
|
<!-- *** BEGIN HTML header *** -->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<HTML><HEAD>
|
|
<title>When Apache Redirect Doesn't Work the First Time LG #61</title>
|
|
</HEAD>
|
|
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
|
|
ALINK="#FF0000">
|
|
<!-- *** END HTML header *** -->
|
|
|
|
<CENTER>
|
|
<A HREF="http://www.linuxgazette.com/">
|
|
<H1><IMG ALT="LINUX GAZETTE" SRC="../gx/lglogo.jpg"
|
|
WIDTH="600" HEIGHT="124" border="0"></H1></A>
|
|
|
|
<!-- *** BEGIN navbar *** -->
|
|
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="nielsen.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue61/nielsen2.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="okopnik.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
|
|
<!-- *** END navbar *** -->
|
|
<P>
|
|
</CENTER>
|
|
|
|
<!--endcut ============================================================-->
|
|
|
|
<H4 ALIGN="center">
|
|
"Linux Gazette...<I>making Linux just a little more fun!</I>"
|
|
</H4>
|
|
|
|
<P> <HR> <P>
|
|
<!--===================================================================-->
|
|
|
|
<center>
|
|
<H1><font color="maroon">When Apache Redirect Doesn't Work the First Time</font></H1>
|
|
<H4>By <a href="mailto:python@kepnet.com">Mark Nielsen</a></H4>
|
|
</center>
|
|
<P> <HR> <P>
|
|
|
|
<!-- END header -->
|
|
|
|
|
|
|
|
|
|
<p><!-- END header -->
|
|
<ol>
|
|
<li>
|
|
<a href="#REF">References</a></li>
|
|
|
|
<li>
|
|
<a href="#Introduction">Introduction</a></li>
|
|
|
|
<li>
|
|
<a href="#problem">The Problem with Redirect</a></li>
|
|
|
|
<li>
|
|
<a href="#perl">Using a Perl script</a></li>
|
|
|
|
<li>
|
|
<a href="#rewrite">Using the mod_rewrite module for Apache</a></li>
|
|
|
|
<li>
|
|
<a href="#redirect">Using Redirect with Virtual Host</a></li>
|
|
|
|
<li>
|
|
<a href="#Conclusion">Conclusion</a></li>
|
|
</ol>
|
|
|
|
|
|
<h3>
|
|
<a NAME="REF"></a>References</h3>
|
|
|
|
<ul>
|
|
<li><a href="http://httpd.apache.org/"> Apache webserver</a></li>
|
|
<li> <a href="http://httpd.apache.org/docs/mod/mod_rewrite.html">Module mod_rewrite
|
|
URL Rewriting Engine</a></li>
|
|
<li> <a href="http://www.perl.com">Perl programming language</a>
|
|
<li><a href="http://www.gnujobs.com/Articles/">Mark's other articles</a>
|
|
<li><a href="http://perl.apache.org/">The Apache/Perl Integration Project</a>
|
|
</ol>
|
|
|
|
|
|
<h3>
|
|
<a NAME="Introduction"></a>Introduction</h3>
|
|
Apache has been a growing project for many years. It is personally
|
|
amazing to me how Apache is so powerful, flexible, and easy to configure for
|
|
a programmer. There is so much documentation about how to configure and do
|
|
things in Apache, other commercial webservers just can't compare. Apache
|
|
makes webserving fun for programmers. There are a lot of things you can test
|
|
and tinker with.
|
|
<p>
|
|
Along with the growing, comes new ways of doing things without getting rid
|
|
of the old ways of doing things. I have a problem with my computer at
|
|
<A HREF="http://gnujobs.com">gnujobs.com</A>.
|
|
Basically, I need to forward every request for 'http://www.tcu-inc.com/mark/articles'
|
|
to 'http://www.gnujobs.com/Articles'. I tried the Apache
|
|
<a href="http://httpd.apache.org/docs/mod/mod_alias.html#redirect">
|
|
Redirect directive</a>, but it didn't work. So, I had to figure out why it didn't work
|
|
and to see if there was any way around it.
|
|
|
|
<h3>
|
|
<a NAME="problem"></a>The Problem with Redirect</h3>
|
|
The problem is, 'Redirect' didn't work for me when I tried to forward
|
|
'http://www.tcu-inc.com/mark/articles' to 'http://www.gnujobs.com/Articles'
|
|
since both websites where running on the same webserver. However, I found out
|
|
later, and it was obvious when I thought about it twice,
|
|
I was using Redirect incorrectly. Nevertheless, it set me on a trip to
|
|
get reacquainted with mod_rewrite. The solutions I had were
|
|
<ol>
|
|
<li> Use a Perl script.</li>
|
|
<li>Use mod_rewrite with Virtual Host</li>
|
|
<li>Correctly use Redirect with Virtual Host</li>
|
|
</ol>
|
|
|
|
|
|
<h3>
|
|
<a NAME="perl"></a>Using a Perl script with Virtual Host</h3>
|
|
This was my first solution after I couldn't get Redirect to work right.
|
|
The only reason why I used it was because it was quick and dirty. It is
|
|
actually fairly complex because you have to understand Perl programming,
|
|
what "Location" does, and how to get a Perl script to execute with your
|
|
Apache webserver. Besides that, it was simple for me to do because I have
|
|
done similar things a million times over.
|
|
<p>
|
|
The nice thing about using a Perl script, is that you don't have to
|
|
recompile the Apache server. You do have to change the configuration slightly.
|
|
You don't have to install mod_perl, but if you do, the configuration
|
|
can be slightly different if you want to cache the perl script.
|
|
Also, this can be done is any programming language, not just Perl.
|
|
<p>
|
|
I had to change the httpd.conf file slightly:
|
|
<pre>
|
|
<VirtualHost 206.21.120.103>
|
|
ServerAdmin info@gnujobs.com
|
|
ServerName www.tcu-inc.com
|
|
DocumentRoot /www/htdocs/
|
|
|
|
ScriptAlias /mark/articles "/www2/TCU.pl"
|
|
</VirtualHost>
|
|
</pre>
|
|
<p>
|
|
The ScriptAlias is the key part here.
|
|
It redirects all requests from /mark/articles
|
|
to the TCU.pl perl script.
|
|
<p>
|
|
And the perl script looked like this,
|
|
<pre>
|
|
#!/usr/bin/perl
|
|
|
|
|
|
print "Location: http://www.gnujobs.com$ENV{'REQUEST_URI'}";
|
|
</pre>
|
|
$ENV{'REQUEST_URI'} is the key part here. It is an environment
|
|
variable that is equal to the file asked for
|
|
on the www.tcu-inc.com webserver. The perl script takes the file asked for, and
|
|
then redirects the browser to the new website.
|
|
|
|
Also, I did a "chmod 755 TCU.pl" on the perl script to make sure it was
|
|
executable.
|
|
<p>
|
|
|
|
|
|
|
|
<h3>
|
|
<a NAME="rewrite"></a>Using the mod_rewrite module for Apache</h3>
|
|
Aaron Bush from <a href="http://www.colug.net">COLUG</a> is responsible for
|
|
getting me to do it in mod_rewrite. I didn't like my Perl solution, and knew
|
|
I could use mod_rewrite to do it, but I was being lazy.
|
|
I knew I should do it in mod_rewrite,
|
|
and Aaron asked me "Why don't you use mod_rewrite?".
|
|
Feeling lazy, I was
|
|
unhappy, for I knew that what separates the men from the boys can be
|
|
issues like this, so I said to myself "screw it, I haven't used mod_rewrite
|
|
in a long time, let me do it the right way".
|
|
<p>
|
|
You have to have mod_rewrite compiled into Apache in order to
|
|
use this option. Mod_rewrite is a nice module that
|
|
"provides a rule-based rewriting engine to rewrite requested URLs on the fly".
|
|
It is a very powerful module, and is essential to learn if you want to become a true
|
|
webmaster or web programmer. It is not essential that you use it, but that
|
|
you know what it can do so that you can present the options to your boss
|
|
when you want to do weird things with your webserver.
|
|
<p>
|
|
I compiled mod_rewrite into Apache and applied this configuration option
|
|
to httpd.conf.
|
|
<pre>
|
|
<VirtualHost 206.21.120.103>
|
|
ServerAdmin info@gnujobs.com
|
|
ServerName www.tcu-inc.com
|
|
DocumentRoot /www/htdocs/
|
|
|
|
RewriteEngine on
|
|
RewriteCond %{HTTP_HOST} ^www\.tcu\-inc\.com$
|
|
RewriteRule ^(/articles)(.*) http://www.gnujobs.com/Articles$2 [R]
|
|
</VirtualHost>
|
|
</pre>
|
|
|
|
RewriteCond sets the condition for which we are going to use mod_rewrite, which
|
|
basically says pay attention to the webserver if www.tcu-inc.com is the base
|
|
website name being used. RewriteRule takes the conditions (everything at www.tcu-inc.com)
|
|
and says that if the requested file starts with "/articles" redirect it to
|
|
www.gnujobs.com. The "$2" corresponds to "(.*)". The rest
|
|
of the requested file after "/articles" is equal to "(.*)". [R] means "Take this
|
|
match and redirect it".
|
|
|
|
|
|
|
|
<h3>
|
|
<a NAME="redirect"></a>Using Redirect with Virtual Host</h3>
|
|
After I figured out how to redirect www.tcu-inc.com to gnujobs.com with
|
|
the other two methods, I realized
|
|
I was doing a mistake with the standard "Redirect". I wasn't putting the
|
|
Redirect command into the VirtualHost section. Once I did that, it worked fine.
|
|
|
|
<pre>
|
|
<VirtualHost 206.21.120.103>
|
|
ServerAdmin info@gnujobs.com
|
|
ServerName www.tcu-inc.com
|
|
DocumentRoot /www/htdocs/
|
|
|
|
Redirect /mark/articles http://www.gnujobs.com/Articles
|
|
</VirtualHost>
|
|
</pre>
|
|
|
|
|
|
|
|
<h3>
|
|
<a NAME="Conclusion"></a>Conclusion</h3>
|
|
I did a lot of work for nothing, but I made this nice article explaining different
|
|
options for other people to learn from. Here are my suggestions about which of these
|
|
3 methods you should use:
|
|
<ol>
|
|
<li> If you don't forsee ever having programmers work on your website, use
|
|
the standard Redirect command.</li>
|
|
<li> If you are a programmer or a true webmaster, then please use mod_rewrite.
|
|
If you standardize on using mod_rewrite, you might find other uses for it as well
|
|
which you might not realize unless you have had previous practice with mod_rewrite. </li>
|
|
<li> I don't know why you would want to use a Perl script to do this, unless you
|
|
want to record certain stats (which are possible in other ways) or do something
|
|
else that is weird. Using a Perl script would just add more overhead. If you
|
|
used it with mod_perl, you might end up using more memory because of caching.
|
|
I wouldn't use a Perl script to do this, although that was my first working
|
|
solution. Bad solutions are good only in the fact that you at least have
|
|
something working.
|
|
</ol>
|
|
|
|
<i> Mark works as an independent consultant donating time to causes like
|
|
<A HREF="http://GNUJobs.com">GNUJobs.com</A>, writing articles, and writing
|
|
free software.</i>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- *** BEGIN copyright *** -->
|
|
<P> <hr> <!-- P -->
|
|
<H5 ALIGN=center>
|
|
|
|
Copyright © 2000, Mark Nielsen.<BR>
|
|
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
|
|
Published in Issue 61 of <i>Linux Gazette</i>, January 2001</H5>
|
|
<!-- *** END copyright *** -->
|
|
|
|
<!--startcut ==========================================================-->
|
|
<HR><P>
|
|
<CENTER>
|
|
<!-- *** BEGIN navbar *** -->
|
|
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="nielsen.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue61/nielsen2.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="okopnik.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
|
|
<!-- *** END navbar *** -->
|
|
</CENTER>
|
|
</BODY></HTML>
|
|
<!--endcut ============================================================-->
|