Forum Moderators: phranque

Message Too Old, No Replies

.htaccess error when trying to correct search function

         

Patrick Taylor

10:34 am on Nov 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a search form on my WordPress site. I have simplified the default WordPress .htaccess file into:


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.+)$ /index.php/$1
</IfModule>
# END WordPress

Everything is working nicely with the exception of the search form, which only delivers a result when the search is made from the homepage.

From the homepage I get a successful search result with:


ht*p://www.site.com/?s=Word+Anotherword

When searching from other pages I am seeing unsuccessful search results, eg:


ht*p://www.site.com/index.php/folder/my-page?s=Word+Anotherword
ht*p://www.site.com/index.php/my-page?s=Word+Anotherword

I've tried various RewriteRules, but either nothing changes or else I see an internal server error. The last one I used was:


RewriteRule ^(index\.php)(/[a-z-/]+)(\?s=)([[:alnum:]+][\+]+)$ /$3$4 [L]

This one produces an internal server error. I don't see why though. In the first bracketed part I've escaped the dot. In the second brackets I am matching any combination of letters, hyphens, and forward slashes - all preceded by a forward slash. In the third brackets I am matching "?s=" and in the fourth, any combination of letters or numbers and plus signs. Obviously there are some blunders somewhere.

The aim is to see in the address bar:


ht*p://www.site.com/?s=whatever+they+searched+for

... no matter which page the search is made from.

jdMorgan

6:45 pm on Nov 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The main problems appear to be:

1) Nested square brackets
2) Attempt to test query string using RewriteRule

Other than that, it appears over-complicated. I'd suggest:


RewriteCond %{QUERY_STRING} ^(s=([0-9a-z]\+)+)$ [NC]
RewriteRule ^index\.php/.+$ /?%1 [L]

Jim

Patrick Taylor

9:55 pm on Nov 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Jim. It did look over-complex. I'm not sure what /?%1 means on the second line though.

I also thought that [[:alnum:]] meant any letter or number. Thanks for the pointer.

jdMorgan

10:39 pm on Nov 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"/" roots the URL-path to you "home page" directory. "?" sets a new query string, and "%1" refers to the matched contents of the first parenthesized sub-expression in the RewriteCond pattern, which is the "s=..." query string.

Jim

Patrick Taylor

10:51 pm on Nov 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks once more... the fog continues to clear.

jdMorgan

1:20 am on Nov 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looking at this again, the original would not accept something like "s=abc+xyz".

This will work better:


RewriteCond %{QUERY_STRING} ^(s=([0-9a-z]\+?)+)$ [NC]
RewriteRule ^index\.php/.+$ /?%1 [L]

In the modified pattern, the "?" makes a trailing "+" optional, and allows for "+" between groups of alphanumerics within the query.

Jim

Patrick Taylor

1:14 pm on Nov 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes I see. I'm still playing about with this - it becomes quite addictive.