Forum Moderators: phranque
I've noticed that there are some very competent people in here who are knowing rewriterules very well.
So I then have to ask one question:
I've a script which reacts on the fiollowing uri:
/index.php?c=manpages&ref=chmod
Now I want to have a RewriteRule so that this should be possible:
/chmod
I've created this Rule:
RewriteRule ^/([a-z0-9_-]+)$ /index.php?c=manpages&ref=$1 [R=301,NC,L]
But this doesn't match something like this "mkfs.ext2". So I've tried several things:
RewriteRule ^/([a-z0-9_-\.]+)$ /index.php?c=manpages&ref=$1 [R=301,NC,L]
RewriteRule ^/([a-z0-9_-[.]]+)$ /index.php?c=manpages&ref=$1 [R=301,NC,L]
But all I'm trying results in an error when restarting apache:
# /etc/init.d/apache start
Configuration syntax error detected, not starting/reloading...
Syntax error on line 219 of /home/dma147/dma147_vhosts.conf:
RewriteRule: cannot compile regular expression '^/([a-z0-9_-\.]+)$' failed!
So... how can I match the dot? ;)
To get that rewrite rule to work you have to escape the -
RewriteRule ^/([a-z0-9_\-\.]+)$ /index.php?c=manpages&ref=$1 [R=301,NC,L] However you then face another problem, as it stands this code will cause an infinite loop because /chmod would redirect to /index.php?c=manpages&ref=chmod which would then redirect to index.php?c=manpages&ref=index.php and try and do this infinitly.
To solve this problem you would have to use a fake sub-directory:
RewriteCond %{REQUEST_URI} ^/manpages
RewriteRule ^/manpages/([a-z0-9_\-\.]+)$ /index.php?c=manpages&ref=$1 [R=301,NC,L] Andrew
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^/([a-z0-9_\-.]+)$ /index.php?c=manpages&ref=$1 [R=301,NC,L]
For later readers: Note that the RewriteRule pattern starts with "/". As such, this code will work in httpd.conf or conf.d, but not in .htaccess. For .htaccess use, remove that leading slash.
Jim