Removing spaces or% 20 in url using htaccess

Hi guys, you can help me here, I have a little problem here to use htaccess to remove all 20% in my url and replace it with a hyphen. I manage to get rid of the remaining 20% โ€‹โ€‹between the words Acer, Liquid, S1, S510

here is my url / localhost / gadgets / product / Acer-Liquid-S1-S510% 20Mobile

As you can see, in the last part there is one% 20, how can I delete it

And here is my 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 

Thanks in advance guys

0
source share
1 answer

Try the following:

 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 

The first rule removes all spaces in the loop (using [N] ), so we donโ€™t need to tell multiple RewriteRules to do this one by one now.

The following rule (with the condition {ENV:Redirect} ) is optional and is used to reflect the use of hyphens in the client browser, as well as to create any bookmark links to the correct version of the URL, other than whitespaced.

+1
source

Source: https://habr.com/ru/post/1215272/


All Articles