Forum Moderators: phranque

Message Too Old, No Replies

Rewrite / Variable Issue

how flexible is mod_rewrite?

         

ilikeitraw

7:11 pm on Jan 12, 2004 (gmt 0)


I have an entry in my .htaccess looking like this:

RewriteRule /([^-]+)-1\.php /brands/brand.php?manu=$1

In making a request to the following URL:
http://www.mydomain.com/MyBrand-1.php

... and doing:
$parray = explode("/", $REQUEST_URI);
echo ($parray[1]);

It prints this to output:
MyBrand-1.php

This is fine, however, if I want to add a new name-value pair to the request, I get strange results... ie:
http://www.mydomain.com/MyBrand-1.php?test=123456

... the resulting page prints out (via the PHP function above):
MyBrand-1.php?test=123456

So, I tried modifying the RewriteRule so it could accept another parameter... ie:

RewriteRule /([^-]+)-1\.php?test=([^0-9]+]) /brands/brand.php?manu=$1&test=$2

... and other variations, but, I can never get the "test" variable passed over to my script correctly. I always end up with:
MyBrand-1.php?test=123456 ... after parsing out the REQUEST_URI in PHP.

I was thinking it would be a lot easier to just pass another parameter along to the next page... but am having a very hard time. Any help would be appreciated!

lorax

7:32 pm on Jan 12, 2004 (gmt 0)

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



Make life a bit easier on yourself and your webpages more SEO friendly.

Example link. www.yourdn.com/file.php?v=3455&x=bubba

Convert links to www.yourdn.com/3455/bubba/file.php

Then use mod-rewrite to parse the URL into the vars and pass them to the file.

learn more [webmasterworld.com]

ilikeitraw

7:47 pm on Jan 12, 2004 (gmt 0)



hey Lorax!

what's wrong with my current setup?
i'm currently using a hyphen as my delimitation like so:
RewriteRule /([^-]+)-1\.php /brands/brand.php?manu=$1

it is working perfectly, as a request to:
/product-1.php sends me to:
/brand.php?manu=product

...but can't figure out how to pass another parameter to the brand.php page.
for example, if this URL was requested:
/product-1.php?test=123456

... it would actually request the /brand.php page with the "test" variable as well as the "manu" variable.

i'm trying to avoid using a directory schema (ie: test/var/var2), and am trying to use a hyphen delimited schema.

lorax

7:56 pm on Jan 12, 2004 (gmt 0)

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



I wouldn't say there's anything wrong with what you're doing (I'm not a pro at regex) but I would say that if you're looking to pass the second var then why not use the directory scheme? Again, from the SEO pov it is better - though, admittedly, SEs are getting better with URLs containing a single var.

Perhaps jdMorgan will stop by and set you straight on the method you started with.

jdMorgan

8:05 pm on Jan 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ilikeit,

To access the first variable in a query string, you'll need to use RewriteCond %{QUERY_STRING} ^test=([^&]*)$ and back-reference that using %1, in the immediately-following RewriteRule.

Jim

ilikeitraw

8:23 pm on Jan 12, 2004 (gmt 0)



JD... thank you, thank you, thank you.
That solution worked out perfectly, and it helped me understand that RewriteCond a lot better.

Many, many thanks!

ilikeitraw

10:00 pm on Jan 12, 2004 (gmt 0)



quick followup...

RewriteCond %{QUERY_STRING} ^pagin=([^&]*)$
RewriteRule /([^-]+)-1\.php /brands/brand.php?keyword=bags&manu=$1&pagin=%1

using the above writeCond/Rule, the "pagin" parameter is passed through correctly, but the "manu" paramter is not.

is the substitution in the RewriteRule ($1) ignored and only the RewriteCond processed... or, is there a problem with the above condition?

ilikeitraw

2:28 am on Jan 13, 2004 (gmt 0)



ok... i'm trying my hardest:

RewriteRule /([^-]+)-1\.php(\?pagin=[0-9]+)* /brands/brand2.php?manu=$1&pagin=$2

In requesting:
/product-1.php?pagin=123456

The "pagin" value doesn't exist on the resulting page (the "manu" parameter is populated perfectly). I've read the Apache doc over and over again, and don't see anything wrong with the above.

Pleeeeeease elighten me!
:D

jdMorgan

9:44 am on Jan 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I've had computer problems for the last three days... :(

It's no good looking to match a query string using RewriteRule - it won't work.

If you could explain what you are trying to do from end-to-end, including the original requested URL-path with query string, the desired output of mod_rewrite, and your overall goal, that would probably help get a better answer. When redirecting to scripts, many things can go wrong -- and often do. A good definition of the problem is needed.

Also, will your rewrite have to deal with missing query parameters, or with the parameters in a different order?

Jim