Rewrite- redirect search result

At least I’ve again studies http://datakoncepts.com/seo and tried before posting …

GOAL:
redirect

http://67.225.255.100/~soffiaw5/blog/?s=SEARCH+SOFFIA+WARDY

to

http://67.225.255.100/~soffiaw5/blog/?s=Soffia+Wardy

the closest I could get (which does NOT work):

RewriteCond %{QUERY_STRING} uniquekey=SEARCH+SOFFIA+WARDY
RewriteRule ^http://67.225.255.100/~soffiaw5/blog/?s=Soffia+Wardy$ /~soffiaw5/blog/?s=$1 [QSA,L]

PURPOSE:
Search Results: Soffia Wardy instead the current Search Results: SEARCH SOFFIA WARDY when somebody clicks onto GO without entering any search term.
however SEARCH SOFFIA WARDY shall remain in the search text-box.

Hopefully somebody can relieve me - my eyes hurt and I’m tired … :confused:

THANK YOU !

Hi Lee,

From yhour code, it looks like you missed the point that a RewriteRule can only access the {REQUEST_URI} - everything from the ? on belongs to the {QUERY_STRING}. so, if

GOAL:
redirect

http://67.225.255.100/~soffiaw5/blog/?s=SEARCH+SOFFIA+WARDY

to

http://67.225.255.100/~soffiaw5/blog/?s=Soffia+Wardy

then you must deal with the {QUERY_STRING} via RewriteCond statements (as you’ve tried - but without removing the query string from the RewriteRule). Therefore, delete the portion in red and your code should work:

RewriteCond %{QUERY_STRING} uniquekey=SEARCH+SOFFIA+WARDY
RewriteRule ^http://67.225.255.100/~soffiaw5/blog/[COLOR="#FF0000"]?s=Soffia+Wardy[/COLOR]$ /~soffiaw5/blog/?s=$1 [QSA,L]

PURPOSE:
Search Results: Soffia Wardy instead the current Search Results: SEARCH SOFFIA WARDY when somebody clicks onto GO without entering any search term.
however SEARCH SOFFIA WARDY shall remain in the search text-box.

If that’s the REAL problem, you should disable the Submit button for as long as “SEARCH SOFFIA WARDY” remains the value for the … uniquekey or s?

Regards,

DK

Thank you David for your reply. However,

RewriteCond %{QUERY_STRING} uniquekey=SEARCH+SOFFIA+WARDY
RewriteRule ^http://67.225.255.100/~soffiaw5/blog/$ /~soffiaw5/blog/?s=$1 [QSA,L]

does not deliver the desired functionality.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~soffiaw5/blog/
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~soffiaw5/blog/index.php [L]
RewriteCond %{QUERY_STRING} uniquekey=Search+Soffia+Wardy
RewriteRule ^http://67.225.255.100/~soffiaw5/blog/$ /~soffiaw5/blog/?s=$1 [QSA,L]
</IfModule>

If that’s the REAL problem, you should disable the Submit button for as long as “SEARCH SOFFIA WARDY” remains the value for the … uniquekey or s?

I know - the search query is that much intertwined with WP code that I failed to do that w/o modifying a core WP function file, which would get overwritten on next update. All other attempts in JD & Php did not work, either.

Hi Lee,

Sorry for the delay - making daily trips to the hospital keeps me busy.

Just a quick scan of your last post, though, and I saw a couple of problems:

  1. The protocol cannot be accessed in a RewriteRule (drop the http://).

  2. The domain name cannot be accessed in a RewriteRule (drop the 67.225.255.100 - and the / if you’re using an Apache 1.x server).

  3. You need to order your RewriteRule sets from most specific to the most generic, i.e., .? will always be matched and redirected to ~soffiaw5/blog/index.php so your query string will never have a chance to match anything.

I hope that helps. I’ll try to look in tomorrow night (after another late night at the hospital) to see if that resolved your problem.

Regards,

DK

Apache 1 and Apache 2 actually behave the same way with the leading slash. See this post where I explained in more detail.

sorry to hear about the hospital trouble - hopefully the doctors and service there is superb!

I tried all steps from above - the closest I could get was a 500 internal server error (“The request was not completed. The server met an unexpected condition.”).

Thank you for all the input - I saved your instructions in my mod-rewrite knowledge file!

The client has by now accepted the current status so I’ll do, too, and simply give up at this point.

Be well,
Lee

Hi Lee,

Thanks for your kind words. Back from the hospital and my wife needs special care for at least 6 weeks. Healing fine, though, so we believe all’s well.

I can’t believe that I left the protocol and domain in the RewriteRule after telling you that you have to remove it! Shows how I was affected.

Without access to the httpd.conf (for a RewriteMap), you’ll have to hard code your capitalization problem (or simply solve it in your receiving script - if you can).

RewriteCond %{QUERY_STRING} uniquekey=SEARCH+SOFFIA+WARDY
RewriteRule ^~soffiaw5/blog/$ /~soffiaw5/blog/?s=Soffia+Wardy [L]

You cannot have the protocol (http://) or the domain (67.225.255.100) referenced in a RewriteRule (regex). Unless you have a RewriteMap (if you do, make use of the tolower built-in function), you’ll have to rewrite your query string manually. Of course, this defeats your search function so either use a dedicated (or VPS) server or make the conversion in PHP.

[indent]If you can make use of tolower, you could use something like

RewriteCond %{QUERY_STRING} ([A-Z]${tolower:[a-zA-Z\ ]+})

to capitalize the first letter of the value string. I’ll let you figure out how to break the query string into words and ucfirst() them with a combination of toupper and tolower if you want mod_rewrite to do the heavy lifting.[/indent]

As for your WordPress code:

# BEGIN WordPress
[COLOR="#FF0000"]# <IfModule mod_rewrite.c> - EVIL AND WASTEFUL!!! Test once without this then get rid of it ... FOREVER!!![/COLOR]
RewriteEngine On
RewriteBase /~soffiaw5/blog/
[COLOR="#FF0000"]# RewriteRule ^index\\.php$ - [L] - that's what the !-f does for you next![/COLOR]

[COLOR="#0000FF"]# <= Insert the above code here and NEVER after WP's generic "redirect everything" code which follows.[/COLOR]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .[COLOR="#0000FF"]?[/COLOR] /~soffiaw5/blog/index.php [L] # I prefer to allow NO path/file

[COLOR="#FF0000"]# RewriteCond %{QUERY_STRING} uniquekey=Search+Soffia+Wardy
# RewriteRule ^http://67.225.255.100/~soffiaw5/blog/$ /~soffiaw5/blog/?s=$1 [QSA,L]
# The REVISED version of this needs to come before the RewriteRule set (rule & conditions) above and after the RewriteBase[/COLOR]
# [COLOR="#FF0000"]</IfModule> - EVIL AND WASTEFUL!!![/COLOR]

My sincere apologies for not having my head about me in the earlier response(s).

Regards,

DK

OMG, so sorry for your poor wife - I really hope she’ll fully recover! In that context even more THANK YOUs for your time and efforts here!

I applied all above code. No 500 error, but I am now sure you are right that for it to work the client would have to upgrade to a self-hosted server with access to the config file to influence the mapping, because unfortunately there is no effect –> http://67.225.255.100/~soffiaw5/blog/?s=Search+Soffia+Wardy:frowning:

This was a great learning experience, and I will also sure heed your tips for all future WP sites :slight_smile:

Blessings to you and your wife,
Lee

Is this still an issue? Seems like this should work:

# if the query string is exactly "s=SEARCH+SOFFIA+WARDY"
# (note that "+" is special in regexs, so we need to escape them)
RewriteCond %{QUERY_STRING} ^s=SEARCH\\+SOFFIA\\+WARDY$

# and if the path is exactly "~soffiaw5/blog/"
# then redirect with a new query string
RewriteRule ^~soffiaw5/blog/$ /~soffiaw5/blog/?s=Soffia+Wardy [R,L]

Thank you, Lee. She’s recovering amazingly quickly but a “cracked” sternum does take up to three months to mend. The bypass part of the operation seems to be trivialized by modern medicine but access makes the recovery long and involved.

As for Jeff’s escaping of the +'s, he’s correct but, after testing, it will work with either an escaped + (i.e., \+) or an escaped space within a character range definition (i.e., [\ ]). As I’m always one for optimizing code, use Jeff’s escaped +.

Tip: Remember that the escaped space will work on spaces in regex where you need to match a space in the variable (e.g., {REQUEST_URI}, {THE_REQUEST} and other places where you can reasonably expect to find a space). mod_rewrite is finicky, though, as a space is used to separate the different elements of mod_rewrite statements so use care!

Regards,

DK