Link to home
Start Free TrialLog in
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.
Avatar of MMDeveloper
MMDeveloper
Flag of United States of America image

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

ASKER

Same thing... I should point out that the search engine is in a sub-directory... would that have any impact on the results?
Excuse me, not the "same thing" but the same results.  Sorry.
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?
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

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?
ASKER CERTIFIED SOLUTION
Avatar of caterham_www
caterham_www
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Odd, the new code still seems to cause the aforementioned problem... any ideas?
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

found my problem!!! I was missing a & from one of the rules.  Thanks again for your help!
very detailed... thanks a lot!!