Apache Server configuration support - Redirect away from Index files

This is Interesting: Free IT Magazines  
Home > Archive > Apache Server configuration support > October 2005 > Redirect away from Index files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Redirect away from Index files
willockguard-newsgoup@yahoo.co.uk

2005-10-25, 5:56 pm

As an inexperienced Apache user, may I ask the following question:

My web site uses Apache 1.3 and php and my Web Host redirects calls to
my web site "www.foo.org.uk" to "www.foo.org.uk/index.html"
or"www.foo.org.uk/index.php".

Index.php uses a mysql database where it loads a relevant page such as
index.php?id=1.

I would like users who type into their browser "www.foo.org.uk/" or even
"www.foo.org.uk"(without the training slash) to
"www.foo.org.uk/index.php?id=1"

Can anyone explain just how would I do that using .htaccess and ReWrite.
My experiments to date enable me to load every call for index.php as
"www.foo.org.uk/index.php?id=1" even where id= any other number.

*******************
RewriteEngine on
RewriteRule ^index\.html$ index.php?id=1
RewriteEngine on
RewriteRule ^index\.php$ index.php?id=1

******************************

With thanks.
--
Stephen Layton


Purl Gurl

2005-10-25, 5:56 pm

willockguard-newsgoup wrote:

> I would like users who type into their browser "www.foo.org.uk/" or even
> "www.foo.org.uk"(without the training slash) to
> "www.foo.org.uk/index.php?id=1"


Give this a try,

RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /index.php?id=1

Use of ^/$ will match your http requests for whatever main
page (index.html) is present. A right hand slash / represents
document root which is most often your htdocs directory.

Note that is a single right hand slash at the beginning of a regex.

This should catch both an URL with a trailing / and without.
Exceptions "might" be if you are using an exotic rewrite to
add a / to the end of an URL address, usually a user URL.

My use of .* /index.php?id=1 will "behave" as this address,

www.foo.org.uk/index.php?id=1

There are shorter syntax for doing this but give my example
syntax a test before moving into shortcut methods; get it
working, then mess around and break it.

A note of caution is your query string ?id=1 will not show
in your access log or other custom logs, unless it is accessed
by a program, php, PERL or other, then written to a log. Don't
panic if you cannot find your query sting in logs; it is there
for your php to use.

Give that syntax a test. Post again if you have problems.

Purl Gurl



willockguard-newsgoup@yahoo.co.uk

2005-10-25, 5:56 pm

Works brilliantly. Many thanks for your reply and explanation.

In message <435EAC59.5000506@purlgurl.net>, Purl Gurl
<purlgurl@purlgurl.net> writes
>willockguard-newsgoup wrote:
>
>
>Give this a try,
>
>RewriteCond %{REQUEST_URI} ^/$
>RewriteRule .* /index.php?id=1
>
>Use of ^/$ will match your http requests for whatever main
>page (index.html) is present. A right hand slash / represents
>document root which is most often your htdocs directory.
>
>Note that is a single right hand slash at the beginning of a regex.
>
>This should catch both an URL with a trailing / and without.
>Exceptions "might" be if you are using an exotic rewrite to
>add a / to the end of an URL address, usually a user URL.
>
>My use of .* /index.php?id=1 will "behave" as this address,
>
>www.foo.org.uk/index.php?id=1
>
>There are shorter syntax for doing this but give my example
>syntax a test before moving into shortcut methods; get it
>working, then mess around and break it.
>
>A note of caution is your query string ?id=1 will not show
>in your access log or other custom logs, unless it is accessed
>by a program, php, PERL or other, then written to a log. Don't
>panic if you cannot find your query sting in logs; it is there
>for your php to use.
>
>Give that syntax a test. Post again if you have problems.
>
>Purl Gurl
>
>
>


--
Stephen Layton




Purl Gurl

2005-10-25, 8:50 pm

willockguard-newsgoup@yahoo.co.uk wrote:
> Purl Gurl writes

(snipped)
[vbcol=seagreen]
[vbcol=seagreen]
> Works brilliantly. Many thanks for your reply and explanation.


Give credit to Ralf S. Engelschall for this. Many years back
he established great guidelines for mod_rewrite which serve
as great lessons for all readers.

If you would like to play, maybe break your rewrite, try this,

RewriteEngine on
RewriteRule ^/$ /index.php?id=1 [R]

That is a shortcut method. I have not tested that syntax but
I am sure it will work. Give it a test and find out! No promise
but might be fun to try.

You only need to turn on the Apache rewrite engine, one time.
Once turned on, it stays on and works for all subsequent
conditions and rules you write. Use RewriteEngine on only
once for any given directory.

Apache rewrite engine can be turned on in your httpd.conf file
but I tend to avoid that, opting for a "per directory" basis
which is inherently more efficient; used only when needed.

This is a good page about mod_rewrite and syntax:

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

You can change the default behavior of Apache to always look
for a php file for your circumstances, but you cannot include
a query string. In your httpd.conf file:

DirectoryIndex index.php index.cgi index.html index.htm

Apache will use index.php first, then use index.cgi next
and right on down the line, until one of those is found.

Research and read about DirectoryIndex for greater details.

For what you are doing, I would lean to not using a rewrite,
using DirectoryIndex, and writing in code which detects a
missing query string, which signals a "first time" access,

if not query string, id equals one

if (!($ENV{QUERY_STRING}))
{ $id = 1; }


Purl Gurl

willockguard-newsgoup@yahoo.co.uk

2005-10-26, 6:07 pm


>(snipped)
>
>If you would like to play, maybe break your rewrite, try this,
>
>RewriteEngine on
>RewriteRule ^/$ /index.php?id=1 [R]
>
>That is a shortcut method. I have not tested that syntax but
>I am sure it will work. Give it a test and find out! No promise
>but might be fun to try.
>
>
>Research and read about DirectoryIndex for greater details.
>
>For what you are doing, I would lean to not using a rewrite,
>using DirectoryIndex, and writing in code which detects a
>missing query string, which signals a "first time" access,
>
>if not query string, id equals one
>
>if (!($ENV{QUERY_STRING}))
> { $id = 1; }
>

Many thanks again, Purl Gurl for your very helpful reply. It was
extremely kind of you to respond in such detail. So far, I haven't got
your shortcut to work, but I'll keep playing.

I'll also take you up on your advice to research DirectoryIndex and
coding. In the meantime, love your web site.
--
Stephen Layton




Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com