Forum Moderators: phranque

Message Too Old, No Replies

rewriteCond & rewriteRules

rewriteCond & rewriteRules issues

         

dlpmdr

8:25 pm on Apr 15, 2012 (gmt 0)

10+ Year Member



let me preface this by saying that any/all help would be GREATLY appreciated. I am on day 4 of trying to troubleshoot this issue. i did not design this, nor am i able to change the scheme... i can only make due with what I've been given.


the short story: I've been handed off this problem to finish up. this is the last issue I have. i have a very old site that used some form of mod-rewrite (no clue as to its exact structure, as the idiots deleted everything weeks ago) which resulted in urls that came in 2 different ways, and looked like this:

http: // www. domainname .com /keyword1/variable1/variable2/variable3
or
http: // www. domainname .com/keyword1/variable1/variable2/variable3/qualifier/variable4

the "qualifier" is one of 4 different strings that told what type of thing variable4 was.



so... i punch together the following htaccess:

rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /keyword1/([^&]+)/([^&]+)/([^&]+)\ HTTP/
rewriteRule ^/new/keyword1/ /new/keyword1/%1/%2/%3/? [R=301,L]

simple. works fine.

that takes care of the first url. the second one is what has been waking me in my sleep for the better part of a week now. I'm coming here as a last ditch effort. the new site is going an astronomical bit more in the background, and so the urls can get complex. but, i would think this would work easily enough (the "all" and "asc" are codes pushed into the new url):

rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /keyword1/([^&]+)/([^&]+)/([^&]+)/qualifier/([^&]+)\ HTTP/
rewriteRule ^/new/keyword1/ /new/keyword1/%1/%2/%3/1/all/all/$4/all/all/all/ASC/? [R=301,L]

but that doesn't work. i have rearranged until i am blue in the face, and just can't get it.

any ideas?

lucy24

11:05 pm on Apr 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you backtrack, please? It will save trouble in the long run, honest. Before delving into mod_rewrite, start by laying out the question in English.

What is the underlying problem, and what do you want to do about it?

Use example.com to prevent examples from auto-linking. But you probably won't need it right away.

dlpmdr

11:16 pm on Apr 15, 2012 (gmt 0)

10+ Year Member



simple redirect. old page with old variables going to new page with new variables.

to be honest, this has fried my brain to the point of no return i think. i just don't give a crap. I've rewritten what i need done in php, and its working flawlessly... so i'll probably just keep it that way.

but, it would be nice to know just what the hell i did wrong (or didn't do)

lucy24

5:47 am on Apr 16, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /keyword1/([^&]+)/([^&]+)/([^&]+)\ HTTP/
rewriteRule ^/new/keyword1/ /new/keyword1/%1/%2/%3/? [R=301,L]

simple. works fine.

You astonish me. Are there query strings involved in this anywhere? If not, what are you looking for in the request? The sequence

([^&]+)/([^&]+)/([^&]+)

is enough to fry anyone's brain. It will work for

/aa/bb/cc

but also for

/aa/bb/cc/dd/ee

/aa/bb/cc/dd/ee/ff/gg/hh/ii/jj et cetera...

In short, for anything containing three or more directory slashes. What are the ampersands doing there anyway? If there's no query-string question mark at the outset, the & would never show its face unless there is something Seriously Wrong. And if you do want to exclude requests with query strings, a simple

... {THE_REQUEST} !\?
will do.

rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /keyword1/([^&]+)/([^&]+)/([^&]+)/qualifier/([^&]+)\ HTTP/
rewriteRule ^/new/keyword1/ /new/keyword1/%1/%2/%3/1/all/all/$4/all/all/all/ASC/? [R=301,L]

...and that's why we always nag at people to explain it in English. (I'll assume $4 here is simply a typo for %4.)

But if you're already fluent in php, there's nothing wrong with that approach. Just make sure that the end result contains only one redirect. Rewrite to php, and let the php issue the one-and-only redirect. Search engines get sulky if they are redirected all over the place.

g1smd

1:05 pm on Apr 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do the old URLs contain query strings? Ampersand is only valid as a query string separator.

Redirecting from one URL to another is usually a simple affair. You'll need [^/]+/ to detect each folder level.