Mod_rewrite Help

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
aceSpec
Forum Newbie
Posts: 12
Joined: Fri Sep 24, 2010 10:13 am

Mod_rewrite Help

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Mod_rewrite Help

Post by John Cartwright »

Untested, but should work fine.
[text]
RewriteEngine On
RewriteCond %{QUERY_STRING} atext=
RewriteRule (.*) /ScrubUtuil.php [R=301][/text]
aceSpec
Forum Newbie
Posts: 12
Joined: Fri Sep 24, 2010 10:13 am

Re: Mod_rewrite Help

Post 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]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Mod_rewrite Help

Post 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]
aceSpec
Forum Newbie
Posts: 12
Joined: Fri Sep 24, 2010 10:13 am

Re: Mod_rewrite Help

Post 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?
aceSpec
Forum Newbie
Posts: 12
Joined: Fri Sep 24, 2010 10:13 am

Re: Mod_rewrite Help

Post 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]
aceSpec
Forum Newbie
Posts: 12
Joined: Fri Sep 24, 2010 10:13 am

Re: Mod_rewrite Help

Post 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?
aceSpec
Forum Newbie
Posts: 12
Joined: Fri Sep 24, 2010 10:13 am

Re: Mod_rewrite Help

Post 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.
Post Reply