Avatar of derek2277
derek2277 asked on

Mod_Rewrite Help

I have a search engine and I am attempting to rewrite the queries.  Here is what I have so far in my .htaccess:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
 
RewriteCond %{REQUEST_URI} /xanga/search.php$
RewriteCond %{QUERY_STRING} ^query=((.*)+)&search=1$
RewriteRule ^(.*)$ /xanga/keywords-%1? [R=301,L]
 
RewriteRule ^keywords-(.*)$ /xanga/search.php?query=$1&search=1&a=1 [L]

***This works fine and displays the url correctly:

ex. http://www.mysite.com/xanga/keywords-myquery*** 

I am running into a problem when I try to send the user to the next page of results.  Apache is not redirecting the url at all:

RewriteCond %{REQUEST_URI} /xanga/search.php$
RewriteCond %{QUERY_STRING} ^query=((.*)+)&search=1&start=([0-9]+)$
RewriteRule ^(.*)/(.*)$ /xanga/keywords-%1/%2? [R=301,L]

RewriteRule ^keywords-(.*)/(.*)$ /xanga/search.php?query=$1&search=1start=$2$a=1 [L]

</IfModule>

I would like to add that if I manually alter the url in the address bar it attempts to search for the entered string: ex. http://www.mysite.com/xanga/keywords-myquery/2 will search the database for "myquery/2"

Anyone know how to fix this?  Please let me know if I need to give more info.
PHPWeb ServersApache Web Server

Avatar of undefined
Last Comment
derek2277

8/22/2022 - Mon
MMDeveloper

try using $1 and $2 instead of %1 and %2
ASKER
derek2277

Same thing... I should point out that the search engine is in a sub-directory... would that have any impact on the results?
ASKER
derek2277

Excuse me, not the "same thing" but the same results.  Sorry.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
caterham_www

Your RegEx doesn't match. But there's no need for .* here, you can restrict it to the specific file (rule-pattern is being processed before conditions are applied). I assume your .htaccess lives in /xanga/.htaccess?
caterham_www

Looks like the code is gone...
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
 
RewriteCond %{QUERY_STRING} ^query=((.*)+)&search=1$
RewriteRule ^search\.php$ /xanga/keywords-%1? [R=301,L]
RewriteRule ^keywords-(.*)$ /xanga/search.php?query=$1&search=1&a=1 [L]
 
RewriteCond %{QUERY_STRING} ^query=((.*)+)&search=1&start=([0-9]+)$
RewriteRule ^search\.php$ /xanga/keywords-%1/%2? [R=301,L]
RewriteRule ^keywords-([^/]+)/(.*)$ /xanga/search.php?query=$1&search=1start=$2$a=1 [L]
 
</IfModule>

Open in new window

ASKER
derek2277

I'm not sure why, but once I use the code provided my server shows increased load until I have to reset it.  My server load usually hovers around 1-3, but right after running the script with the mod_rewrite it jumps to 10ish then consistently increases until it reaches about 100-115!  I tested it three times and it happened each time... any ideas why?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
caterham_www

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
derek2277

Odd, the new code still seems to cause the aforementioned problem... any ideas?
ASKER
derek2277

I have to apologize, there was another problem that must have been induced from the old code... but that has been fixed now.  So far, the code that you provided has almost completed what I was looking to do... here is the issue: Everything works fine until I click the link to go to the next page... it shows the correct url in the address bar (http://www.mysite.com/keywords/key+terms/65) but it still shows the results from the first page of results.  Almost there!

Also, I have moved the search over to the main directory instead of the sub.  Here is the updated code:
<IfModule mod_rewrite.c>
 
Options +FollowSymLinks
RewriteEngine On 
 
RewriteCond %{QUERY_STRING} ^query=([^&]+)&search=1$
RewriteRule ^search\.php$ /keywords/%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^query=([^&]+)&search=1&start=([0-9]+)$
RewriteRule ^search\.php$ /keywords/%1/%2? [R=301,L]
 
RewriteRule ^keywords/([^/]+)/([0-9]+)$ /search.php?query=$1&search=1start=$2&a=1 [L]
RewriteRule ^keywords/(.*)$ /search.php?query=$1&search=1&a=1 [L]
 
</IfModule>

Open in new window

ASKER
derek2277

found my problem!!! I was missing a & from one of the rules.  Thanks again for your help!
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
derek2277

very detailed... thanks a lot!!