Page 1 of 1
Mod_rewrite Help
Posted: Fri Sep 24, 2010 10:23 am
by aceSpec
Hey all,
i've relatively new to the mod_rewrite module, but i've been looking at this thing for a whole work day. Im trying to redirect an page based on whether or not the incoming url has a specific text in the query string. In my .htaccess, here is what I have:
Code: Select all
RewriteCond %{QUERY_STRING} ^.*(atext=).*$
RewriteRule . /ScrubUtuil.php [R]
I've tried using R, R=201, and/or L. When ever it use L , i get an internal server error, if I use R or R=301, it redirects to my error page not my ScrubUtil.php.
Escentially its not going to ScrubUtil.php. Its either going to my error page or I get a internal server error.
Thanks in adance.
Re: Mod_rewrite Help
Posted: Fri Sep 24, 2010 10:52 am
by John Cartwright
Untested, but should work fine.
[text]
RewriteEngine On
RewriteCond %{QUERY_STRING} atext=
RewriteRule (.*) /ScrubUtuil.php [R=301][/text]
Re: Mod_rewrite Help
Posted: Fri Sep 24, 2010 11:06 am
by aceSpec
Thanks. Im getting somethign now..lol...im using firefox and im getting
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Is it possible that when it redirects to my ScrubUtil page, its passing the querystring and thus redirecting again to itself?
It this a correct update
Code: Select all
RewriteRule !^(.*)/ScrubUtil.php/(.*)$ /ScrubUtil.php [R=301]
Re: Mod_rewrite Help
Posted: Fri Sep 24, 2010 11:47 am
by John Cartwright
Hmm I'm not sure why the query string is being carried over to the redirect (it shouldn't), but thats the only explanation for an infinite loop.
A simple fix would be to add the query operator to the redirect.
[text]RewriteEngine On
RewriteCond %{QUERY_STRING} atext=
RewriteRule (.*) /ScrubUtuil.php? [R=301][/text]
Re: Mod_rewrite Help
Posted: Fri Sep 24, 2010 1:48 pm
by aceSpec
The process is being redirected,but Im getting a 404.
I have an ErrorDocument 404 <somepage>.
The redirect is going to that sompage.
my .htaccess file is under /var/www/html, actually all me pages are on this level. Do I need to setup the RewriteBase?
Re: Mod_rewrite Help
Posted: Fri Sep 24, 2010 2:25 pm
by aceSpec
Ok, your method works, with putting the '?' after my redirect URL.
The thing is, I need the query strings that are being sent in, but I don't want it to throw that error.
Im trying to write a cond to check that 'ScrubUtil.php' is not in the PATH_INFO. If its not then run the rul. ScrubUtil.php will never be called by itself. Can you see what im missing or advise another way to redirect with a query string?
Code: Select all
RewriteCond %{QUERY_STRING} atext=
RewriteCond %{PATH_INFO} !=ScrubUtil.php
RewriteRule (.*) /ScrubUtil.php [R=301]
Re: Mod_rewrite Help
Posted: Mon Sep 27, 2010 9:35 am
by aceSpec
Just looking for more help on this.
In my .htaccess file, I was testing this
Code: Select all
RewriteCond %{PATH_INFO} scrubutil\.php
I was trying to say "If the path_info has "scrubutil.php" in it, then run my rewrite rule. This worked (I think). If I went to index.php?atext=1, it would just go to my index page. If I went to ScrubUtil.php?atext=1, it would run my php page but not throw a recursion error.
Then I tried adding ! in front of my regex
Code: Select all
RewriteCond %{PATH_INFO} !scrubutil\.php
When I did this, all my pages threw back (from firefox)
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Any ideas?
Re: Mod_rewrite Help
Posted: Mon Sep 27, 2010 9:40 am
by aceSpec
SOLVED-ish
Here is what I just ended up with:
Code: Select all
RewriteCond %{QUERY_STRING} atext=
RewriteCond %{REQUEST_URI} !scrubutil\.php [NC]
RewriteRule (.*) /ScrubUtil.php [R=301]
Adding my querystring requirement from any page, redirects to my scrubutil.php, and I don't get that error message.
the "ish" page is that I can still do this from my scriptutil.php, and i get the same result...but I can look into that later.
Thanks.