简体   繁体   中英

.htacces redirect when $1 is empty

I'm building an online shop and I'm trying to redirect every shop subfolder to the standard shop.php page displaying the results filtered by the url path as a category.

the structure is mydomain.com/shop/category where category is a field stored on the database.

I use the following php code for filtering the results:

$url = isset($_GET["url"]) ? $_GET["url"] : false;

if (!$url)
    $where_products = "WHERE 1";
else
    $where_products = "WHERE c.url = '$url'";

/* list products filtered by where clause */ 
$sql_products = "SELECT p.name FROM `product` p INNER JOIN `category` c ON p.id_category = c.id $where_products LIMIT 0,80";

now I have the following .htaccess file for redirecting the subfolder to the correct url variable:

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^shop/([^/\.]+)/?$ shop.php?url=$1 [L]

now everything works fine, but when I try to show all products using mydomain.com/shop/ I get the follwing error message

/shop/ was not found on this server

what can I do on the .htaccess file to redirect correctly all products when no subfolder is called?

Tweak your regex to support 0 or more characters instead of 1 or more characters .

Try this rule:

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^shop/([^/.]*)/?$ /shop.php?url=$1 [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM