mod_rewrite help
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!
-
Hello, Have you tried any of the suggestions listed here: Reference: mod_rewrite, URL rewriting and "pretty links" explained They break it down pretty well. 0 -
Try the following rule in your ".htaccess" file, it will replace all space characters (\s or %20) to hyphen - 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]0
Please sign in to leave a comment.
Comments
2 comments