mod_rewrite: avoid similar URLs go to the same page.php
I'm trying to work with mod_rewrite
without particular problems but this:
RewriteRule ^(product/)([^/\<\>].+?)(/)([^/\<\>].+?)(/edit)/?$ php/prd_edit.php
RewriteRule ^(product/)([^/\<\>].+?)(/)([^/\<\>].+?)(/components/edit)/?$ php/prd_comp_edit.php
the first URL
is for this kind of contents when I'd like to edit the main infos:
http://site.com/product/Nintento/Wii/edit => php/prd_edit.php
the second URL
is when I'd like to edit the list of compone开发者_Python百科nts inside the products
http://site.com/product/Nintento/Wii/components/edit => php/prd_comp_edit.php
If I digit the second URL
, mod_rewrite
redirect the requesto to prd_edit.php
instead of prd_comp_edit.php
, because it read Wii/components
as a product name.
I thougt to avoid this by use ([^/\<\>].+?)
to specify the product can't have chars like /
but it still read the product name as Wii/components
.
How can I fix this?
First, move the second rewrite first and add [L] flags to them, this will fix your problem and here are some more optimisations.
RewriteRule ^product/(.+?)/(.+?)/components/edit/?$ php/prd_comp_edit.php?cat1=$1&cat2=$2 [L]
RewriteRule ^product/(.+?)/(.+?)/edit/?$ php/prd_edit.php?cat1=$1&cat2=$2 [L]
Now, given your example, $_GET['cat1'] == "Nintendo"
and $_GET['cat2'] == "wii"
RewriteCond %{REQUEST_URI} ^/product
RewriteRule /components/edit/?$ php/prd_comp_edit.php [L]
RewriteRule /edit/?$ php/prd_edit.php [L]
This way you will be able to have deeper nested product groups as well :D
EDIT
$uri = $_SERVER['REQUEST_URI'];
// Split the URI apart using explode or any other suitable method
$parts = explode('/', rtrim($uri, '/'));
// Now throw away any part that is not connected to a product
// You know that the URI started with product and also know
// that it ended with components and/or edit
// $x = file is prd_comp_edit.php ? 3 : 2
$parts = array_slice($parts, 1, count($parts) - $x);
// Now there you have it, the products hierarchy ready to be processed further
精彩评论