Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

special character replacement in wordpress search function

replace + with _


rich cardona

11:02 pm on Jun 16, 2013 (gmt 0)

I'm using a WordPress plugin that I really like to show custom tables, but the query function doesn't work quite right. The developer hasn't been very helpful, but I think if I can just get this redirect to work, I should be good.

When searching for a term such as "mill creek" the URL turns the spaces into + and the results are skewed. But if one enters an underscore instead of a space, the search results are precisely correct.

Therefore, I'm trying to use mod rewrites to change this:


into this:

I've attempted dozens of iterations for days, and so far, this is the best I've got:

RewriteRule ^(wcts.*)\+(.*)$ $1_$2 [R=301,N,L]

but it doesn't work. Any advice? Thank you in advance.

[edited by: phranque at 12:47 pm (utc) on Jun 17, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]


3:41 am on Jun 17, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

For starters: Are you aiming for a rewrite or a redirect? Your rule as written is a half-hearted redirect: [R=301] flag, but target doesn't include explicit protocol-plus domain.


Surprisingly, this is almost what you want. (The "wcts" seemed so improbable, I assumed you meant [wcts] until I poked into those auto-linked URLs.) I say "almost" because mod_rewrite alone doesn't see the query string, only the path.

Second issue: What if the question contains more than one literal space? You can make RewriteRules to cover, separately, a limited number of possible spaces. But if you venture into tail-that-wags-the-query territory, htaccess is not what you want to do. Don't rely on the [N] flag. In fact it's potentially disastrous because [N] doesn't mean "keep running this same rule until it rinses clean". It means "go all the way back to the first RewriteRule and start over".

You need two things. The pattern itself should give the name of the page, minus query:
RewriteRule ^dev/?$
Note that the ? here means "optional". Nothing to do with the query string. It's in case the final directory-slash got lost.

Then there will be a condition in the form
RewriteCond %{QUERY_STRING} (?:^|&)wcts=([^&+]+)\+([^&+]+)(?:$|&)

If you want to make versions of the rule for queries with two or three literal spaces, create a separate rule-with-condition set for each. More than three spaces and you really shouldn't be doing it in htaccess.

Is the "wcts" part always the first thing in the query? The combination of recycling part of the query while changing another part will be much more painless if you've ensured that the pieces of the query string always come in the same order.

But before we go any further...

the URL turns the spaces into +

What exactly do you mean by this? That is, I understand that literal spaces are being turned into literal plus signs. But at what stage does this happen? Normally you don't see it until the query is sent out into the internet-- the same point where spaces in the path turn into %20. Here it seems as if the query ought to get intercepted before it can leave your site. If it wasn't supposed to go anywhere, the problem may be something different entirely.

rich cardona

4:33 pm on Jun 17, 2013 (gmt 0)

Thank you so much for your reply, Lucy. I really appreciate your help. The WordPress plugin for custom tables provides a search form you can see at the bottom of the page here:


When I enter a phrase, "mill creek" for example, I see a URL that looks like this in my browser:


The results I get show me anything with "mill" or "creek" but not "mill creek". I understand that the proper way to handle this would be through the plugin, probably by changing the code that submits the form, but since the developer seems to have lost interest, I thought apache rewrites might be the answer for me and others. Another user of this plugin (I'm not the only one with this problem) made the discovery that a URL that reads:


will provide results for "mill creek". It's the same as the former URL generated with the form submit except that the + character is replaced manually by an underscore. It seems onerous to ask a user to enter underscores instead of spaces in their search terms in order to see the correct results.

In spite of my pathetic attempt to write the proper rule, I've probably spent some 15-20 hours on this issue, so I'm really very grateful.


8:57 pm on Jun 17, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Nothing to do but swear at the developer, then :) This is generally useful and therapeutic in any case, so no harm done. While doing so, go back to my previous post and pick out all specific, concrete questions. Before we can hammer out an optimal rule we'll need some more answers.

Featured Threads

Hot Threads This Week

Hot Threads This Month