mod_rewrite Filter
February 7, 2007 2:22 PM   Subscribe

mod_rewrite filter: I need some code foo help here. I'm running a website that was just converted to WordPress. There are hundreds of Google search results pointing to the old site's search system. I want to redirect those to the new system.

So, basically, I want a call to:

http://ask.metafilter.com/search/?query=banana

to redirect to

http://ask.metafilter.com/?s=banana

Any help?
posted by GatorDavid to Computers & Internet (14 answers total) 3 users marked this as a favorite
 
I think this (or something very like it) should do you:

RewriteEngine On
RewriteBase /
Redirectmatch /search/?query=(.*) /?s=$1 [L]
posted by cortex at 2:45 PM on February 7, 2007


(I'm presuming incautiously that you know where to stick that. Let me know if that was a bad move.)
posted by cortex at 3:27 PM on February 7, 2007


Response by poster: Yes, I've played with .htaccess quite a bit. I am able to do all sorts of other neat tricks with it. I've just been having a #$@!% of a time with this one.

I'll give this a shot. Thanks!
posted by GatorDavid at 3:45 PM on February 7, 2007


Response by poster: Bummer. Didn't work. Here's the full contents of my current .htaccess file:

----------------------------------------
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

redirect 301 /rss.xml /feed/
----------------------------------------

I added your code immediately after the "RewriteBase /" line but it seemed to have no effect.
posted by GatorDavid at 3:50 PM on February 7, 2007


How about this something like this (before the RewriteConds, and after the RewriteBase):

RewriteRule /search/\?query\=(.*) /\?s\=$1 [L]

I'm not sure the ? and the = need to be escaped, but it should't hurt.
posted by team lowkey at 4:07 PM on February 7, 2007


Response by poster: Thanks, team lowkey, but that didn't work either. I'm really stumped.
posted by GatorDavid at 4:13 PM on February 7, 2007


Response by poster: Since I can't seem to find a mod_rewrite solution, I just created a "search" directory and stuck an index.php in it that handles the "query" querystring parameter and prepends the 301 header().

Does anyone think that's a crazy, stupid way to do it?
posted by GatorDavid at 4:28 PM on February 7, 2007


This seems like it should be a no-brainer. Is something else going on? Can you temporarily disable all the other lines in the .htaccess file except for cortex's first snippet and test that? (Is mod_rewrite disabled?? or maybe take a look in your error_log?)
posted by misterbrandt at 4:31 PM on February 7, 2007


Maybe it doesn't like the leading slash, since the base already includes it. Maybe this:

RewriteRule ^search/\?query\=(.*) \?s\=$1 [L,R]

...or since you're working at the root level anyway, maybe just get rid of the RewriteBase and use:

RewriteRule ^/search/\?query\=(.*) /\?s\=$1 [L,R]

Are your other rules working?
posted by team lowkey at 4:32 PM on February 7, 2007


Response by poster: misterbrandt:
The rest of the mod_rewrite rules work fine and -- unfortunately -- I can't turn them off or it will hose the whole site. I'm sorry to say that I have no idea how to view the error_log. I'm only an apache n00b. I can do just enough to get myself into trouble.

team lowkey:
tried both of those and still no dice.

Using the "search" directory with the index.php in there seems to be doing the trick, though.
posted by GatorDavid at 4:35 PM on February 7, 2007


Caveat for WordPress + Apache, btw: WP likes to overwrite the contents of the

#BEGIN WordPress
...
#END WordPress

block with, you know, whatever it feels like; it's a good idea to maintain other directives in a seperate block outside of the BEGIN / END demarcations.
posted by cortex at 4:46 PM on February 7, 2007


Come on guys. You can't match anything in the QUERY_STRING like that. What you need is something like

RewriteCond %{QUERY_STRING} query=([^&]+)
RewriteRule ^/search/ /?s=$1 [R,L]
posted by Rhomboid at 5:03 PM on February 7, 2007


It might be \1 instead of $1, I can never remember which is which.
posted by Rhomboid at 5:06 PM on February 7, 2007


Rhomboid has it. Anything including/after a '?' in a URL is not part of the URL but is instead called the "query string" or sometimes the 'GET' dictionary (i.e. in PHP it's $_GET and in other frameworks it's request.GET or similar).

In other words, the ?foo=bar&biz=baz junk part of a 'URL' is a paramaterization of a request to the actual URL - that's how HTTP works. You've got a resource at a URL path at a given domain, and you have the option of setting GET (or POST, when submitting a form) parameters when requesting that resource.

So in this case, you don't want to change the URL, which stays the same - you want to change the query string instead.

Yea, I think it's $1.
posted by cyrusdogstar at 8:05 AM on February 8, 2007


« Older Logging applications using WMI   |   To Call or Not to Call... Newer »
This thread is closed to new comments.