views:

305

answers:

1

I used to have the following rule in my .htaccess while building a website:

RewriteRule ^test/(.*) cgi-bin/router.py?request=$1 [NC]

Whenever the user goes to mysite.com/test/something they get redirected to cgi-bin/router.py?request=something. This worked very well. I have now finished the site and want to 'turn it on'. However simply removing the test/ bit from the rule does not work, although it should according to here and here:

RewriteRule ^(.*) cgi-bin/router.py?request=$1 [NC]

I have also tried ^$ but then I loose whatever it was the user typed in the url.

Have I gotten my regex wrong?

+1  A: 

Try adding a / in front of it, and a $ at the end.
Like:

RewriteRule ^/(.*)$ cgi-bin/router.py?request=$1 [NC]

Also, I find it best to use the RewriteBase directive when testing. Then you can just alter that instead of having to mess with the regular-expressions themselves.

RewriteBase /test/ 
RewriteRule ^(.*)$ cgi-bin/router.py?request=$1 [NC]

And, when moved to the real-world:

RewriteBase /
RewriteRule ^(.*)$ cgi-bin/router.py?request=$1 [NC]

Edit
After some more testing, I found that the problem is perhaps not with the expression itself, but rather with your directory name: cgi-bin/. Apache, by default, uses this directory for CGI scripts, but uses ScriptAlias to redirect requests for /cgi-bin/* to another directory. In my case a directory named the same in the Apache installation directory.

Is this a directory you created yourself, or are you using the directory Apache has aliased this to?
You have to either

  1. Use the directory Apache specifies in the configuration
  2. Edit the configuration to allow you to specified this yourself, or
  3. Choose another directory name.

Personally, I would go with #3. I tried this, and it worked fine for me.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} !bin/
    RewriteRule ^(.+) bin/request.php?ctrl=$1 [NC,L]
</IfModule>
Atli
Using the first option I still need to enter the slash for the rule to work: mysite.com//something
Pavel
And using RewriteBase doesn't have any impact.
Pavel
Ok, I see. I came up with another possibility. Added it to the post.
Atli
Adding `RewriteCond %{REQUEST_URI} !cgi-bin/*` did the trick. thank you.
Pavel