simplify url rewrite
am 06.11.2007 16:12:00 von lfoss1
I am wondering if there is a way to simplify the following 3 url
rewrites into one. I am new to URL rewrites, so I am not sure what the
limitations are.
RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\|(.*)$
RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
storeId=10001&catalogId=%4&partNumber=%2-%3&langId=%5&parent ProductId=
%6&parentCategoryId=%7 [PT,L]
RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\|?$
RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
storeId=10001&catalogId=%4&partNumber=%2-%3&langId=%5&parent ProductId=
%6 [PT,L]
RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)$
RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
storeId=10001&catalogId=%4&partNumber=%2-%3&langId=%5 [PT,L]
Basically, the last 2 parts of the URL coming in are optional. I was
hoping that I could narrow this down to 2 line instead of 6.
Thanks,
Re: simplify url rewrite
am 06.11.2007 16:40:23 von Jim Hayter
lfoss1@yahoo.com wrote:
> I am wondering if there is a way to simplify the following 3 url
> rewrites into one. I am new to URL rewrites, so I am not sure what the
> limitations are.
>
> RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\|(.*)$
> RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
> storeId=10001&catalogId=%4&partNumber=%2-%3&langId=%5&parent ProductId=
> %6&parentCategoryId=%7 [PT,L]
>
> RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\|?$
> RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
> storeId=10001&catalogId=%4&partNumber=%2-%3&langId=%5&parent ProductId=
> %6 [PT,L]
>
> RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)$
> RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
> storeId=10001&catalogId=%4&partNumber=%2-%3&langId=%5 [PT,L]
>
>
> Basically, the last 2 parts of the URL coming in are optional. I was
> hoping that I could narrow this down to 2 line instead of 6.
Perhaps this (untested):
RewriteRule
^/i/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?([^\|]*)\|?([^/ ]*)/?$
/webapp/wcs/stores/servlet/ProductDisplay?storeId=10001&cata logId=%4&partNumber=%2-%3&langId=%5&parentProductId=%6&paren tCategoryId=%7
[PT,L]
NOTE: wrapping introduced by client, the above is one line. I'm not
sure if the escape is needed in the "([^\|]*)" piece.
I use similar rules for rewrites where some parts are optional.
HTH,
Jim
Re: simplify url rewrite
am 06.11.2007 23:01:28 von HansH
"Jim Hayter" schreef in bericht
news:13j12n8r8hie566@news.supernews.com...
> lfoss1@yahoo.com wrote:
>> I am wondering if there is a way to simplify the following 3 url
>> rewrites into one. I am new to URL rewrites, so I am not sure what the
>> limitations are.
>>
>> RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\|(.*)$
>> RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
>> storeId=10001&catalogId=%4&partNumber=%2-%3&langId=%5&parent ProductId=
>> %6&parentCategoryId=%7 [PT,L]
>>
>> Basically, the last 2 parts of the URL coming in are optional. I was
>> hoping that I could narrow this down to 2 line instead of 6.
>
> Perhaps this (untested):
> RewriteRule
> ^/i/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?([^\|]*)\|?([^/ ]*)/?$
> /webapp/wcs/stores/servlet/ProductDisplay?storeId=10001&cata logId=%4&partNumber=%2-%3&langId=%5&parentProductId=%6&paren tCategoryId=%7
> [PT,L]
>
> NOTE: wrapping introduced by client, the above is one line. I'm not sure
> if the escape is needed in the "([^\|]*)" piece.
>
Changed back-references type to matches at condition of rule.
Modified rule to match optional last pair more strictly
RewriteRule
^/i/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)(/([^\|]*)(\|([^/ ]*))?)?/?$
/webapp/wcs/stores/servlet/ProductDisplay?storeId=10001&cata logId=$4&partNumber=$2-$3&langId=$5&parentProductId=$7&paren tCategoryId=$9
NOTE: unsure about reference number when nested
HansH
Re: simplify url rewrite
am 06.11.2007 23:45:32 von Jim Hayter
HansH wrote:
> Changed back-references type to matches at condition of rule.
> Modified rule to match optional last pair more strictly
Good point. I don't mix multiple delimiters so I don't generally have
that problem.
If you find out how nested references are numbered, I be interested to know.
Jim
Re: simplify url rewrite
am 09.11.2007 17:37:26 von Rik Wasmus
On Tue, 06 Nov 2007 16:12:00 +0100, wrote:
> I am wondering if there is a way to simplify the following 3 url
> rewrites into one. I am new to URL rewrites, so I am not sure what the=
> limitations are.
>
> RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\|(.*)$
> RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
> storeId=3D10001&catalogId=3D%4&partNumber=3D%2-%3&langId=3D% 5&parentPr=
oductId=3D
> %6&parentCategoryId=3D%7 [PT,L]
>
> RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)\|?$
> RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
> storeId=3D10001&catalogId=3D%4&partNumber=3D%2-%3&langId=3D% 5&parentPr=
oductId=3D
> %6 [PT,L]
>
> RewriteCond %{REQUEST_URI} ^/i/(.*)/(.*)/(.*)/(.*)/(.*)$
> RewriteRule ^.*$ /webapp/wcs/stores/servlet/ProductDisplay?
> storeId=3D10001&catalogId=3D%4&partNumber=3D%2-%3&langId=3D% 5 [PT,L]
>
>
> Basically, the last 2 parts of the URL coming in are optional. I was
> hoping that I could narrow this down to 2 line instead of 6.
The question is: do you really need it? It will depend on the actual =
script/code receiving thir. For instance, I see a lot of people struggli=
ng =
with rewrites to get everything in the query string, while for instance =
=
for PHP script a simple parsing of the $_SERVER['REQUEST_URI'] would wor=
k.
An illustration, I have a very basic .htaccess for a site build with PHP=
=
(from memory, typos can occur..):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(products|articles|categories)/.*$ /includes/$1.php [L,QSA=
]
And in PHP I'll just examine the structure like this:
$urlinfo =3D parse_url($_SERVER['REQUEST_URI']);
$request =3D explode('/',$urlinfo['path']);
foreach($request as $key =3D> $value){
echo "Pseudo dir $key was $value\n";
}
?>
Which for /products/foo/bar/foz will give me something like:
Pseudo dir 0 was products
Pseudo dir 1 was foo
Pseudo dir 2 was bar
Pseudo dir 3 was foz
Optionally, you can throw the complete request in one GET variable:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /path/to/script.ext?request=3D$1 [L,QSA]
And just let your script examine the request GET variable.
In almost any case a script able to read GET variables and acting on it =
=
will have a lot more abilities and power to process an URL/strings then =
a =
simple regex in mod_rewrite, highly increasing flexibility, =
maintainability and possibilities. A drawback is that you delegate the =
responsibility for generating error responses like 404 to the script.
-- =
Rik