Htaccess clears urls and replaces spaces and% 20 -

I'm struggling to do this job. Currently my htaccess contains the following code:

#Debugging - Error reporting php_flag display_startup_errors on php_flag display_errors on php_flag html_errors on #Commpression <ifmodule mod_deflate.c=""> <filesmatch ".(js|css|html|png|jpg|jpeg|swf|bmp|gif|tiff|ico|eot|svg|ttf|woff|pdf)$"=""> SetOutputFilter DEFLATE </filesmatch> </ifmodule> Options All -Indexes +FollowSymLinks -MultiViews <IfModule mod_rewrite.c> # Turn mod_rewrite on RewriteEngine On RewriteBase / #RewriteCond %{THE_REQUEST} (\s|%20) RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI] RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI] #RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^.*\.(png|jpg|bmp|gif|css|js)$ [NC] RewriteRule ^([^/]+/?.+)$ /index.php?req=$1 [L,QSA] </IfModule> 

Everything works fine, except for 1 thing, if I try this URL, for example:

 http://www.domain.com/ test/ 

the browser translates it like this: http://www.domain.com/%20test/ mainly after the domain, if the path starts with a space or% 20 it fails. can anyone point out a solution where the leading spaces will be removed?

UPDATE

Goal:

 www.domain.com/ this is a test / hello there / 

or

 www.domain.com/ this is a test 

to

www.domain.com/this-is-a-test/ or www.domain.com/this-is-a-test/hello-there

+3
source share
1 answer

I am guilty of writing this code more than two years ago: P

This can be greatly simplified by this code:

 # remove spaces from start or after / RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L] # remove spaces from end or before / RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L] # replace spaces by - in between RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R] 

PS: It should be added that you need to fix the source of these URLs, since it really is not the case to get URLs like this.

+9
source

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


All Articles