重写规则数组隐藏 url 中的类别

Rewrite rules array hide the category from url

我正在使用下面的重写规则数组来重写 URLs。

$rules = array(
    'image'             => "/image/(?'image'[\w\-]+)",                        // '/image/image-slug'
    'product-category'  => "/product-category/(?'product_category'[\w\-]+)",  // '/product-category/category-slug'
    'product'           => "/product/(?'product'[\w\-]+)",                    // '/product/product-slug'
    'post'              => "/(?'post'[\w\-]+)",                               // '/post-slug'
    'home'              => "/"                                                // '/'
);

下面是我的 .htaccess。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} baqsa\.pk [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.baqsa.pk/ [R,L]
</IfModule>

当前的 URL 就像 domain.com/product-category/category-slug 等。我想从 URL 中删除或隐藏 product-category。我想要这样 URL domain.com/category-slug 当我从这里的数组中删除 product-category'product-category' => "/(?'product_category'[\w\-]+)", 然后它工作正常但是 post 'post' => "/(?'post'[\w\-]+)", 不工作..我在这里缺少什么?

根据您显示的示例,您能否尝试以下操作。将 your_site 更改为您的实际域名,同时请确保在测试您的 URL 之前清除浏览器缓存。

RewriteEngine ON

RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://www.your_site%{REQUEST_URI} [R=301,NE,L]

RewriteRule ^product-category/(.*)/?$  [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]