Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite only looking at last two parameters

mod_rewrite not working

         

chuender

8:06 pm on May 29, 2004 (gmt 0)

10+ Year Member



Hi,

I am trying to get:
[mySite.com...]

To redirect to:
[mySite.com...]

The parameters "display", "by" and "model_no" are not always used by pages.

This is my .htaccess code:
RewriteEngine on
RewriteRule ^index(.*)/([^/]+)(.*)$ index$1$3?model_no=$2
RewriteRule ^index(.*)/([^/]+)(.*)$ index$1$3?by=$2 [QSA]
RewriteRule ^index(.*)/([^/]+)(.*)$ index$1$3?display=$2 [QSA]
RewriteRule ^index(.*)/([^/]+)/([^/]+)(.*)$ index?fuseaction=$2.$3 [QSA]
RewriteRule ^index index.php [L]

I have several problems:
1. When I use www.mySite.com/index/products/showMain, I get to the right page but some texts are missing.
2. When I use [mySite.com...] the .htaccess uses the last two parameters, "parameters" and "6308PT" as the fuseaction (the second to the last rule). It should be using the first two rules because these are the last two parameters in the URL, no?

I tried the following:
[mySite.com...]

And .htaccess ignores the blah blah blah's and only match the last two parameters and index.

Please help.

Thanks.

Chuender

[edited by: jdMorgan at 5:52 pm (utc) on May 31, 2004]
[edit reason] Removed specifics per TOS [/edit]

gergoe

4:18 pm on May 30, 2004 (gmt 0)

10+ Year Member



Try

RewriteEngine on
RewriteRule ^index/([^/]+)/([^/]+)(.*)$ index$3?fuseaction=$1.$2 [NC]
RewriteRule ^index/([^/]+)(.*)$ index$2?display=$1 [NC,QSA]
RewriteRule ^index/([^/]+)(.*)$ index$2?by=$1 [NC,QSA]
RewriteRule ^index/([^/]+)(.*)$ index$2?model_no=$1 [NC,QSA]
RewriteRule ^index/? index.php [NC,L]

It is not tested, but it should do the trick.
Working with Regular Expressions can be difficult sometimes, needs a lot of attention and some experience also. I suggest you to find some resources about this topic.

chuender

7:14 am on May 31, 2004 (gmt 0)

10+ Year Member



Hi gergoe,

That still didn't work... the server is not matching anything now, less than before.

It must be the pattern of the first 3 rules that the server does not understand:
RewriteRule ^index(.*)/([^/]+)(.*)$ index$1$3?model_no=$2
RewriteRule ^index(.*)/([^/]+)(.*)$ index$1$3?by=$2 [QSA]
RewriteRule ^index(.*)/([^/]+)(.*)$ index$1$3?display=$2 [QSA]

But I am very not sure what to change... I've played with several patterns already.

Thanks.

gergoe

10:57 am on Jun 1, 2004 (gmt 0)

10+ Year Member



I was curious so I've added the one i sent you to my test server, and it was working well, the test page always got the right parameters. I suggest you to give it a try again, try to add the RewriteBase / directive in the front of your rules, add the

RewriteCond %{REQUEST_URI} !^index\.php$

directive in front of the last RewriteRule and come back with the result, it might needs to be changed a bit according to the environment you have, but it should be working.

chuender

9:52 pm on Jun 5, 2004 (gmt 0)

10+ Year Member



Hi gergoe,

Thanks for the input, however, apache is still only recognizing the last two parameters, no matter how many slashes are in the URL.

Please advise.

Thanks.

jdMorgan

4:26 am on Jun 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is a brute-force solution.

This code will not work if the parameters are not in fixed order. Since the parameters are not 'tagged' in any uniquely-identifiable way, they are identifiable only by their position in the requested URL, and there is no way to tell which one might be missing if their order is not fixed.

If the order is not fixed, and any one or more of the parameters might be missing, then there is no solution using position alone; You will either have to 'find' each parameter based on a list of possible values (this is 'ugly' because this list will have to be maintained and fully re-tested after any change), include the tags in the URL (like 'http://example.com/index/products/showList/dsp_pH/by_parameter/mod_6308BT'), or modify the site's pages so that all parameters are always included in fixed order, even if blank, i.e. 'http://example.com/index/products/showList///6308PT'.

Basically, you have to give mod_rewrite some way to uniquely identify each parameter in the URL. If you can't do that, then mod_rewrite cannot and won't work reliably. At that point, you have a site-architecture design problem, not a mod_rewrite problem.


RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?fuseaction=$1.$2&display=$3&by=$4&model_no=$5 [L]
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?fuseaction=$1.$2&display=$3&by=$4 [L]
RewriteRule ^index/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?fuseaction=$1.$2&display=$3 [L]
RewriteRule ^index/([^/]+)/([^/]+)/?$ /index.php?fuseaction=$1.$2 [L]

This code has been successfully tested in .htaccess on my server with the following URLs:
http://example.com/index/products/showList/pH/parameters/6308PT/
http://example.com/index/products/showList/pH/parameters/6308PT
http://example.com/index/products/showList/pH/parameters/
http://example.com/index/products/showList/pH/parameters
http://example.com/index/products/showList/pH/
http://example.com/index/products/showList/pH
http://example.com/index/products/showList/
http://example.com/index/products/showList

One more comment: Do not use ".*" unless you absolutely have to. ".*" is 'greedy' and will match the maximum number of characters possible. For example, with the pattern "^(.*)/(.*)(.*)$" and given a requested URL of "/index/a/b/c/d", $1 will contain "index/a/b/c", $2 will contain only "d", and $3 will always be blank, because the first ".*" will consume as much of the string as possible, and the second one will take whatever is left, leaving the third empty. This can and does often lead to unexpected results. I have used "[^/]+" in the patterns above to mean "one or more characters not equal to a slash" for this reason. This also makes the regex parsing much faster and unambiguous.

Jim

chuender

5:39 pm on Jun 6, 2004 (gmt 0)

10+ Year Member



Hi Jim,

For some reason your code did not work on my machine, but it did give me great options to explore. This is how it (finally) worked:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index(.*)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)(.*)$ index?fuseaction=$2.$3&display=$4&by=$5&model_no=$6 [QSA]
RewriteRule ^index(.*)/([^/]+)/([^/]+)/([^/]+)/([^/]+)(.*)$ index?fuseaction=$2.$3&display=$4&by=$5 [QSA]
RewriteRule ^index(.*)/([^/]+)/([^/]+)(.*)$ index?fuseaction=$2.$3 [QSA]
RewriteRule ^index index.php [L]

Not really sure what happened, but I would sure like to know why the this code worked.

Thanks for everyone's help in making this a fun-filled (frustrating) mod_rewrite experience.

Chuender

gergoe

7:00 pm on Jun 7, 2004 (gmt 0)

10+ Year Member



The only thing why the suggested rewrites were not working could be that the url you've posted in your first post is not complete/correct, or if you have additional rewrites in your httpd.conf or in a htaccess file. The way you use the .* expression is more like to the situation when you're building a garage for your car, but you did not measure your car, so to be on the safe side you'll make it 10 meters long and 10 meters wide, but the car will not fit into it because it is not high enough...

chuender

7:21 pm on Jun 7, 2004 (gmt 0)

10+ Year Member



Hi Gergoe,

That's a very interesting analogy. I am still trying to figure out why I have to use the .* in my statements, but the URLs posted in the first message are corrct (I can cut and paste the URLs into the address field of IE, change mySite to the real domain and access the right page on the company website), I do not have rewrites in httpd.conf, and I do not have additional rewrite in my .htaccess file.

This is the main reason for my frustrations, especially since the rewrite statements you and Jim posted worked in your environments.

Thanks.

Chuender

gergoe

7:51 pm on Jun 7, 2004 (gmt 0)

10+ Year Member



Why not to ask your hosting provider to temporary turn on the logging for mod_rewrite and see why the suggested examples aren't working, or why the one with the .* expressions are working?