kdn

Registered
Jan 8, 2020
1
0
1
Iceland
cPanel Access Level
Website Owner
Hi all,

wondering if any experts out there can help me, this is what I want to achieve:

when someone goes to this page: example.com/help.php?page=help%20topic%20one (yes the real page name has spaces!!)
they get rewritten to a friendly URL: example.com/help/help-topic-one

I have got the easy bit done like so:
RewriteCond %{THE_REQUEST} \s/help\.php\?page=(.*)\s [NC]
RewriteRule ^ /help/%1? [R=302,L]
RewriteRule ^help/(.*)$ /help.php?&page=$1 [L]

that redirects the page successfully, but the get string still contains the %20's, anyone know what I can add to my rule to rewrite them to dashes aswell?





thanks in advance!
 

24x7server

Well-Known Member
Apr 17, 2013
1,912
99
78
India
cPanel Access Level
Root Administrator
Twitter
Try the following rule in your ".htaccess" file, it will replace all space characters (\s or %20) to hyphen -

Code:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]

# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]