使用htaccess删除url中的空白或%20


Removing whitespaces or %20 in url using htaccess

嗨,伙计们,你们能帮我吗?我在这里遇到了一点麻烦,我正在尝试使用htaccess删除我url中的所有20%,并用连字符替换它。我设法删除了Acer、Liquid、S1、S510 之间的其他20%

这是我的urllocalhost/ggadgets/product/Acer-Liquid-S1-S510%20Mobile

正如你所看到的,在最后一部分有一个%20,我该如何删除它

这是我的htaccess

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /gadgets/
Options -Indexes
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1
RewriteRule ^product/([A-Za-z0-9-]+)/?$   product.php?product_name=$1-$2 [NC,L]
RewriteRule ^(.*/|)['s%20]+(.+)$ $1$2 [L]
RewriteRule ^(.+?)['s%20]+(/.*|)$ $1$2 [L]
RewriteRule ^([^'s%20]*)(?:'s|%20)+(.*)$ $1-$2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteRule ^(.*)$ $1.php

提前感谢各位

试试这个:

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /gadgets/
RewriteRule ^([^'s%20]*)(?:'s|%20)+(.*)$ $1-$2 [N,E=Redirect:1]
RewriteCond {ENV:Redirect} ^1$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1 [NC,L]
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}'.php -f
RewriteRule ^(.*)$ $1.php

第一条规则删除循环中的所有空白(使用[N]),因此我们现在不必指定多个RewriteRules逐个执行。

下一条规则(带{ENV:Redirect}条件)是可选的,用于反映客户端浏览器上连字符的使用,以便创建的任何书签都链接到正确的非空白URL版本。