Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!



Photo
- - - - -

What's Wrong With My Rewritecond Or Rewriterule?


  • Please log in to reply
4 replies to this topic

#1 rolf

rolf

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 675 posts
  • Location:Suffolk UK

Posted 13 March 2010 - 04:04 AM

I thought I was getting my head around .htaccess rewrites after a few recent successes but I can't figure out what I'm doing wrong here -

CODE
RewriteCond %{REQUEST_URI} ^/shopping-index\.php\?catnum=9 [NC]
RewriteRule ^(.*)$ /shopping-index\.php\?catnum=2 [R=301,L]


for context - I've merged categories 9 and 2, moving all of the category 9 products into category 2. I don't want to throw away the momentum the category 9 page has, so I'm trying to 301 requests for that page.

My hunch is that I've made a mistake in the pattern recognition of rewritecond but I've tried all the tweaks I can think of and nothing is solving it.

Any help would be much appreciated :-)

#2 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 13 March 2010 - 11:48 AM

It's a common error Rolf, so don't beat yourself up over it. REGEX is just weird. I'm still trying to wrap my mind around it, and I've been working with it practically every week for 15 years! lol.gif

A couple of things to remember for this one.

{REQUEST_URI} only refers to the actual physical file portion of the url path. Not the query string and not even the hostname. To do both the uri path and query string you'd need two rewritecond's strung together.

Also, remember that query strings are going to appended to the new url in the 2nd part of the rewriterule by default action, so you need to tell Apache not to do that with a ? at the end of the new url so you don't end up with two query strings in the url.

So a quick and dirty example would look like.

CODE
RewriteCond %{REQUEST_URI} ^/shopping-index\.php [NC]
RewriteCond %{QUERY_STRING} ^catnum=9
RewriteRule ^(.*)$ /shopping-index.php?catnum=2? [R=301,L]


#3 rolf

rolf

    HR 6

  • Active Members
  • PipPipPipPipPipPip
  • 675 posts
  • Location:Suffolk UK

Posted 15 March 2010 - 10:34 AM

Thanks randy - that helps a lot! Is it OK if I admit that I'm beginning to develop a real dislike of REGEX? lol.gif

I notice there are no escape characters in your rewrite rule - is that an optional thing or would that have been causing a problem too? As much as escaping the period I'm trying to figure out how it knows which question mark to pay attention to and how not to fall foul of that in the future.

Cheers ale.gif

#4 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 15 March 2010 - 11:33 AM

QUOTE
Is it OK if I admit that I'm beginning to develop a real dislike of REGEX?


Yes.

I love it and hate it at the same time. lol.gif

QUOTE
I notice there are no escape characters in your rewrite rule - is that an optional thing or would that have been causing a problem too?


As a very general rule RewriteCond lines should always have special characters escaped. So \. instead of simply .

RewriteRules have two parts. Special characters in the first part should be escaped for sure. But as a general rule do not have to be and should not be in the second part of this line.

#5 1dmf

1dmf

    Keep Asking, Keep Questioning, Keep Learning

  • Active Members
  • PipPipPipPipPipPipPip
  • 2,161 posts
  • Location:Worthing - England

Posted 16 March 2010 - 10:07 AM

QUOTE
I love it and hate it at the same time.
agreed, Regular Expressions are extremely powerful and at the same time extremely confusing!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

SPAM FREE FORUM!
 
If you are just registering to spam,
don't bother. You will be wasting your
time as your spam will never see the
light of day!