You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by John Nichel <jo...@kegworks.com> on 2006/04/25 19:00:20 UTC

[users@httpd] mod rewrite redirect if NOT in URL

Hi, I usually have no problem redirecting based on what is in the URL, 
but now I'm trying to redirect all pages in a site to one page on the 
same site. I have to exclude that one page from the redirection process 
else I get an infinite loop. I've tried numerous combinations of the 
following, but I can't see to get it to work....

RewriteCond %{SCRIPT_FILENAME} !^redirect\.html [NC]
RewriteRule ^/$ /redirect.html


Basically, if the URL doesn't contain 'redirect.html', redirect to 
redirect.html

TIA
-- 
John C. Nichel IV
Dot Com Holdings of Buffalo
716.856.9675
jnichel@dotcomholdingsofbuffalo.com

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] mod rewrite redirect if NOT in URL

Posted by Joshua Slive <jo...@slive.ca>.
On 4/25/06, John Nichel <jo...@kegworks.com> wrote:

> Hi Joshua,  thanks for the response.  Unfortunately it doesn't seem to
> be working for me.  Right now I have this :
>
> RewriteEngine On
> RewriteBase /
> RewriteCond %{Request_URI} !^/redirect\.html [NC]
> RewriteRule ^/$ /redirect.html
>
> And none of the pages I go to on the site get redirected.  I set up the
> rewrite log, and nothing is being entered into there (tailing it).  To
> ensure that Apache was executing my .htaccess file, I put some bogus
> code in it and it thru an error, so that part's working.

If there is nothing in the RewriteLog, then you haven't configured it
correctly or your aren't putting your directives in the right place. 
It is always easiest to do mod_rewrite stuff in the main server or
<VirtualHost> context, and not in <Directory> sections or .htaccess
files.  This eliminates a lot of complications.

Also, as I already mentioned, something is screwy above.  RewriteRule
^/$ matches only a request for "/" and nothing else.  If you are using
it in an .htaccess file, it very likely matches nothing since the
leading slash gets stripped.  Perhaps you want RewriteRule .* instead.

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] mod rewrite redirect if NOT in URL

Posted by Krist van Besien <kr...@gmail.com>.
On 4/25/06, John Nichel <jo...@kegworks.com> wrote:
> Joshua Slive wrote:
> > On 4/25/06, John Nichel <jo...@kegworks.com> wrote:
> >> Hi, I usually have no problem redirecting based on what is in the URL,
> >> but now I'm trying to redirect all pages in a site to one page on the
> >> same site. I have to exclude that one page from the redirection process
> >> else I get an infinite loop. I've tried numerous combinations of the
> >> following, but I can't see to get it to work....
> >>
> >> RewriteCond %{SCRIPT_FILENAME} !^redirect\.html [NC]
> >> RewriteRule ^/$ /redirect.html
> >>
> >>
> >> Basically, if the URL doesn't contain 'redirect.html', redirect to
> >> redirect.html
> >
> > (I suspect your example is off, since the RewriteRule above will not
> > create a loop; it only addresses one URL.)
> >
> > Use the RewriteLog to figure out what is going wrong.
> >
> > Either of these should work:
> > RewriteCond %{Request_URI} !^/redirect\.html [NC]
> > ...
> > or
> > RewriteRule ^/redirect\.html - [L]
> >
> > Joshua.
> >
>
> Hi Joshua,  thanks for the response.  Unfortunately it doesn't seem to
> be working for me.  Right now I have this :
>
> RewriteEngine On
> RewriteBase /
> RewriteCond %{Request_URI} !^/redirect\.html [NC]
> RewriteRule ^/$ /redirect.html
>
> And none of the pages I go to on the site get redirected.  I set up the
> rewrite log, and nothing is being entered into there (tailing it).  To
> ensure that Apache was executing my .htaccess file, I put some bogus
> code in it and it thru an error, so that part's working.

I often have a need to exclude something from rewriting, and I ususaly
do it by having a rule that would match it, but that does not change
it, and giving it the L flag.

Your problem I would solve like this:

RewriteRule ^/redirect∖.html$ /redirect.html [L]
RewriteRule ^/.*$                       /redirect.html

(Nortice also how your rule
RewriteRule ^/$ /redirect.html
would only match /, not every page)

Krist

--
krist.vanbesien@gmail.com
Solothurn, Switzerland

Re: [users@httpd] mod rewrite redirect if NOT in URL

Posted by John Nichel <jo...@kegworks.com>.
Joshua Slive wrote:
> On 4/25/06, John Nichel <jo...@kegworks.com> wrote:
>> Hi, I usually have no problem redirecting based on what is in the URL,
>> but now I'm trying to redirect all pages in a site to one page on the
>> same site. I have to exclude that one page from the redirection process
>> else I get an infinite loop. I've tried numerous combinations of the
>> following, but I can't see to get it to work....
>>
>> RewriteCond %{SCRIPT_FILENAME} !^redirect\.html [NC]
>> RewriteRule ^/$ /redirect.html
>>
>>
>> Basically, if the URL doesn't contain 'redirect.html', redirect to
>> redirect.html
> 
> (I suspect your example is off, since the RewriteRule above will not
> create a loop; it only addresses one URL.)
> 
> Use the RewriteLog to figure out what is going wrong.
> 
> Either of these should work:
> RewriteCond %{Request_URI} !^/redirect\.html [NC]
> ...
> or
> RewriteRule ^/redirect\.html - [L]
> 
> Joshua.
> 

Hi Joshua,  thanks for the response.  Unfortunately it doesn't seem to 
be working for me.  Right now I have this :

RewriteEngine On
RewriteBase /
RewriteCond %{Request_URI} !^/redirect\.html [NC]
RewriteRule ^/$ /redirect.html

And none of the pages I go to on the site get redirected.  I set up the 
rewrite log, and nothing is being entered into there (tailing it).  To 
ensure that Apache was executing my .htaccess file, I put some bogus 
code in it and it thru an error, so that part's working.

-- 
John C. Nichel IV
Dot Com Holdings of Buffalo
716.856.9675
jnichel@dotcomholdingsofbuffalo.com

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] mod rewrite redirect if NOT in URL

Posted by Joshua Slive <jo...@slive.ca>.
On 4/25/06, John Nichel <jo...@kegworks.com> wrote:
> Hi, I usually have no problem redirecting based on what is in the URL,
> but now I'm trying to redirect all pages in a site to one page on the
> same site. I have to exclude that one page from the redirection process
> else I get an infinite loop. I've tried numerous combinations of the
> following, but I can't see to get it to work....
>
> RewriteCond %{SCRIPT_FILENAME} !^redirect\.html [NC]
> RewriteRule ^/$ /redirect.html
>
>
> Basically, if the URL doesn't contain 'redirect.html', redirect to
> redirect.html

(I suspect your example is off, since the RewriteRule above will not
create a loop; it only addresses one URL.)

Use the RewriteLog to figure out what is going wrong.

Either of these should work:
RewriteCond %{Request_URI} !^/redirect\.html [NC]
...
or
RewriteRule ^/redirect\.html - [L]

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org