We help IT Professionals succeed at work.

Mod_Rewrite Help

402 Views
Last Modified: 2013-12-12
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.
Comment
Watch Question

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

Author

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

Author

Commented:
Excuse me, not the "same thing" but the same results.  Sorry.
Top Expert 2008

Commented:
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?
Top Expert 2008

Commented:
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

Author

Commented:
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?
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Odd, the new code still seems to cause the aforementioned problem... any ideas?

Author

Commented:
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

Author

Commented:
found my problem!!! I was missing a & from one of the rules.  Thanks again for your help!

Author

Commented:
very detailed... thanks a lot!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.