Welcome to WebmasterWorld Guest from 54.205.20.160

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Strip Query String Only from One Script

   
9:37 pm on Jul 2, 2008 (gmt 0)

5+ Year Member



Hello,

I have searched through the forum here but haven't been able to figure out my rewrite rule.

I have a script located at http://www.example.com/scripts/do_something.php

I'd like to strip all parameters from do_something.php so that:

http://www.example.com/scripts/do_something.php?name=filename&location=loc&blah=blah

301 redirects to

http://www.example.com/scripts/do_something.php

I do not, however, want any other scripts with query strings to be stripped, only do_something.php.

Thanks!

10:09 pm on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



This is a fairly simple mod_rewrite. Please post your best-effort code as a basis for discussion. See our forum charter for more information.

Thanks,
Jim

2:06 am on Jul 3, 2008 (gmt 0)

5+ Year Member



I have tried:

RewriteCond %{query_string} .
rewriterule (.*) http://www.example.com/$1? [R=301,L]

but this strips query strings from all files, not just the do_something.php file.

Then I tried:

RewriteCond %{query_string} .
rewriterule ^scripts/do_something.php?name=(.*) http://www.example.com/scripts/do_something.php$ [R=301,L]

And many more variations of the above two. I seem to create an infinite redirect often because I am redirecting from do_something.php with query strings to do_something.php without query strings.

2:26 am on Jul 3, 2008 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Try:

RewriteRule ^/scripts/do_something\.php\?.+$ /scripts/do_something.php [R=301,L]
2:40 am on Jul 3, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Assuming this code goes in .htaccess and not in a server config file, you almost got it, actually:

RewriteCond %{QUERY_STRING} .
RewriteRule ^scripts/do_something\.ph[b]p$[/b] http://www.example.com/scripts/do_something.ph[b]p?[/b] [R=301,L]

A query string is never visible to RewriteRule, so a RewriteCond must be used, and the "?" at the end of the substitution URL is the operator that actually clears the query string; Without that operator, the query string will be passed-through the rule unchanged.

Jim

7:19 am on Jul 3, 2008 (gmt 0)

5+ Year Member



you know with programming, you can be so close ("almost got it") yet so far unless you have some help. thank you for the help!