PDA

View Full Version : Mod-rewrite with contingencies. Please help.


Blitzen
11-17-2005, 01:09 AM
I need to make the following mod_rewrites in the same file. Your help is greatly appreciated. I spent hours on trying to figure this out.

This mod_rewrite needs all the mentioned flags as you will understand after reading the contingencies.

Here goes.

(1) If the url contains "/cat-", NOT "/ns", and NOT "/cs", then rewrite from
/cat-mycat1.html
To
/cgi-bin/myscript.cgi?pg=frog&ref=cat-mycat1

(2) If the url contains "/cat-" and "/ns", and NOT "/cs" , then rewrite from
/cat-mycat1/ns20.html
To
/cgi-bin/myscript.cgi?pg=frog&ref=cat-mycat1&num=20

(3) If the url contains "/cat-" and NOT "/ns" and has "/cs", then rewrite from
/cat-mycat1/csHOMEdog:cat-mycat2.html
To
/cgi-bin/myscript.cgi?pg=frog&ref=cat-mycat1&tree=HOMEdog:cat-mycat2
NOTE: The duplicate "cat-" are not the same "cat-". One is the variable "cat-mycat1" and the other is "cat-mycat2".

(4) If the url does not contain "/cat-" but contains either "/a-" or "/p-"
And it contains "/cs" but NOT "/ns" then rewrite (the "a-" condition is given in the example)
/a-myPage1/csHOMEdogcat-mycat2.html
to
/cgi-bin/mysript.cgi&pg=bird&ref=a-myPage1&tree=HOMEdogcat-mycat2

(5-end) etc. for (either "/a-" or "/p-") plus and minus "/cs" and '/ns".

If I see the answer for the above puzzle, I think that I can finish the logic.
Thanks again for letting me get a half nights sleep.

SanDiegoSEO
11-17-2005, 12:14 PM
I'd love to know if these work but:


RewriteRule /(.*).html$ /myscript.cgi?pg=frog&ref=$1
RewriteRule /(.*)/ns(.*).html$ /myscript.cgi?pg=frog&ref=$1&num=$2
RewriteRule /(.*)/(.*).html$ /myscript.cgi?pg=frog&ref=$1&tree=$2

Blitzen
11-17-2005, 03:35 PM
Hi SanDiegoSEO,
Thank you for the reply.

I need to know the RewriteCond also.

For the RewriteCond, I also need to know how to capture the first "/cat-mycat1" in the RewriteCond instead of the "cat-mycat2" that is later in the url.

Also, is there a way to determine what the exact url is being written?

I tried the following (with more) and it doesn't work.
(3) If the url contains "/cat-" and NOT "/ns" and has "/cs", then rewrite from
/cat-mycat1/csHOMEdog:cat-mycat2.html
To
/cgi-bin/myscript.cgi?pg=frog&ref=cat-mycat1&tree=HOMEdog:cat-mycat2
===================
RewriteCond %{REQUEST_FILENAME} /cat-
RewriteCond %{REQUEST_FILENAME} !^/ns$
RewriteCond %{REQUEST_FILENAME} /cs
RewriteRule ^(.*)/cs(.*)\.html$ /cgi-bin/myscript.cgi?pg=frog&ref=$1&tree=$2 [L]
===================

I'd love to know if these work but:


RewriteRule /(.*).html$ /myscript.cgi?pg=frog&ref=$1
RewriteRule /(.*)/ns(.*).html$ /myscript.cgi?pg=frog&ref=$1&num=$2
RewriteRule /(.*)/(.*).html$ /myscript.cgi?pg=frog&ref=$1&tree=$2

SanDiegoSEO
11-17-2005, 03:43 PM
I've never used RewriteCond when doing rewrites www.krtraining.info is the latest site I've done one on, and the htaccess file looks like this:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/index.html doggie.php?Page_ID=$1


Which turns:
http://www.k9training.info/doggie.php?Page_ID=dog-trainer

into:
http://www.k9training.info/dog-trainer/index.html

I would believe that these rules would need to be in the root but have:
RewriteRule /(.*).html$ /cgi-bin/myscript.cgi?pg=frog&ref=$1
RewriteRule /(.*)/ns(.*).html$ /cgi-bin/myscript.cgi?pg=frog&ref=$1&num=$2
RewriteRule /(.*)/(.*).html$ /cgi-bin/myscript.cgi?pg=frog&ref=$1&tree=$2

I'm a trail by error type person, so to be positive I'd have to test a few things.

SanDiegoSEO
11-17-2005, 03:45 PM
Wait, are you trying to break up variables to determine the rule?? I don't think thats possible.

I think you have the capture the variable as a whole, and you cannot break it up.

Your URL:
/cgi-bin/myscript.cgi?pg=frog&ref=cat-mycat1&tree=HOMEdog:cat-mycat2

is broken into the base:
/cgi-bin/myscript.cgi?

And variables:
"variable" = "value"
pg=frog
&ref=cat-mycat1
&tree=HOMEdog:cat-mycat2

Maybe I'm misunderstanding what you're trying to do, but it sounds like you want to break up the values to determine which rule is used.

The two "cat-" are parts of variable values,

Blitzen
11-17-2005, 04:04 PM
There are two types of pages, pg=cat or pg=prod.
Each page has a reference, e.g., ref=somepage1 or ref=somepage1.
The categories (cat) reference values start with "cat-".
The items (pg=prod) ref starts with either "p-" or "a-". The rewrites for the items will come after the "/cat-" pages are analyzed.

The "/ns" refers to the page number if the number of items per page exceeds the maximum displayed. This appears in the URLs only if there are more items than the page displays.

The "/cs" refers to the parent categories for that particular reference. That is why you will see "cat-mycat1" or "cat-mycat2" following the "/cs" flag.
If this is a top level category, there will be no "/cs" flag.

Thus, I need to have a mod_rewrite for each contingency. I thought that I needed the RewriteCond to sort out what the url contains before I execute the RewriteRule.

If I am going the wrong direction, please help steer me right.

seomike
11-18-2005, 10:47 AM
Your RewriteCond aren't working because you should be using REQUEST_URI

You really don't need conditions in this rewrite since the pattern matching in the rewriterules can detect the different variations.

RewriteEngine On
RewriteBase /

#rewrites cat-var.html
RewriteRule ^cat-([^/]+)\.html$ cgi-bin/myscript.cgi?pg=frog&ref=cat-$1 [L]

# rewrites cat-var1/nsvar2.html
RewriteRule ^cat-([^/]+)/ns([^/]+)\.html$ cgi-bin/myscript.cgi?pg=frog&ref=cat-$1&num=$2 [L]

# rewrites cat-var1/csvar2.html
RewriteRule ^cat-([^/]+)/cs([^/]+)\.html$ cgi-bin/myscript.cgi?pg=frog&ref=cat-$1&tree=$2 [L]

#rewrites a-myPage1/csHOMEdogcat-mycat2.html
RewriteRules ^a-([^/]+)/cs([^/]+)\.html cgi-bin/mysript.cgi&pg=bird&ref=a-$1&tree=$2 [L]

As per your last urls just give me some examples of what they look like. Instead of trying to explain the logic.

SanDiegoSEO
11-18-2005, 11:56 AM
ahhh, so you can split the value if it has a common pattern.

Blitzen
11-18-2005, 12:47 PM
SEOMike got it! Thanks for the help!
"Your RewriteCond aren't working because you should be using REQUEST_URI"

SEOMike is also correct about not using the RewriteCond. I forgot one important permutation in my initial question. Consider the following:

both "/ns" and "/cs" were in the URL.
Wouldn't the "# rewrites cat-var1/nsvar2.html" apply to this in SEOMike's solution?

Anyway, I got it working with your excellent help!