Forum Moderators: phranque
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]
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]
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.
RewriteCond %{REQUEST_URI} !^index\.php$
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]
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
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
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