I have the following code in .htaccess and it works for pages which have a name of just one word. But it fails for page names consisting of more than one word.
RewriteEngine on
RewriteRule ^(.*)/$ /index.php?page=$1 [L]
I use the ending slash to indicate that the url needs to be rewritten, to avoid messing up real filenames.
The problem might have to do with the fact that spaces are url encode to %20.
Any suggestion on how to alter the regex or .htaccess in general?
The RewriteRule documentation includes a list of flags that you can set. It’s always worth reading in cases like this.
It seems that you may need the B flag:
The B flag tells Apache to escape nonalphanumeric characters in substituted backreferences, which apparently is what you want.
Also, adding the
RewriteConds as above provides a safer way to avoid rewriting the URLs of real file names (such as images) that are being requested, as Michael says.