.htaccess 通过htaccess删除url中的某些元素

yeotifhr  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(182)

通过htaccess,我想删除url中的这个点:产品/描述示例:https://test.com/Products/Description/logitech/Id-12
我试过这个,但它不起作用。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Products/Description/$1$2 [L]

下面是我完整的htaccess尝试过的规则文件:

RewriteEngine On
DirectorySlash Off

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L]

# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)//+[?\s]
RewriteRule ^ %1 [R=302,L]

# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

# Remove Products/Description ===> not working
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Products/Description/$1$2 [L]
yyhrrdl8

yyhrrdl81#

根据您展示的示例和努力,请尝试以下.htaccess规则文件。我们需要对它做一些更改。请确保您的.htaccess文件与Products文件夹位于沿着(不是在它里面,而是在它旁边)。
此外,请确保在测试URL之前清除浏览器缓存。

RewriteEngine ON

RewriteBase /Products/Description/

DirectorySlash Off

# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]

# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)//+[?\s]
RewriteRule ^ %1 [R=302,L]
RewriteCond %{THE_REQUEST} \s/index\.php\?([^&]*)&([^&]*)&Id=(\d+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Products)/(Description)/(\d+)/?$ index.php?$1&$2&Id=$3 [QSA,NC,L]

# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

相关问题