Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule problem on apache-1.3.34

         

dma147

11:01 am on May 1, 2006 (gmt 0)

10+ Year Member



Hello,

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? ;)

Little_G

11:23 am on May 1, 2006 (gmt 0)

10+ Year Member



Hi,

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

dma147

11:37 am on May 1, 2006 (gmt 0)

10+ Year Member



Hi Little_G,

hmm... that's a pity... :(
But well, thanks for your help. It works great. ;)

jdMorgan

12:25 pm on May 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...Or exclude the destination URL from being rewritten:
 
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^/([a-z0-9_\-.]+)$ /index.php?c=manpages&ref=$1 [R=301,NC,L]

This rule will be invoked only for requested URLs that are NOT "index.php".

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

dma147

2:54 pm on May 1, 2006 (gmt 0)

10+ Year Member



Cool... that's much better! Thanks a lot!