apache 根目录的Apache rewriteRule
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1806554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache rewriteRule for root directory
提问by Pavel
I used to have the following rule in my .htaccess while building a website:
我曾经在构建网站时在我的 .htaccess 中有以下规则:
RewriteRule ^test/(.*) cgi-bin/router.py?request= [NC]
Whenever the user goes to mysite.com/test/somethingthey 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 hereand here:
每当用户访问时,mysite.com/test/something他们都会被重定向到cgi-bin/router.py?request=something. 这非常有效。我现在已经完成了该网站并想“打开它”。然而,简单地test/从规则中删除该位是行不通的,尽管它应该根据这里和这里:
RewriteRule ^(.*) cgi-bin/router.py?request= [NC]
I have also tried ^$but then I loose whatever it was the user typed in the url.
我也尝试过,^$但后来我丢失了用户在 url 中输入的任何内容。
Have I gotten my regex wrong?
我弄错了正则表达式吗?
回答by Atli
Try adding a /in front of it, and a $at the end.
Like:
尝试/在它前面添加一个,并$在最后添加一个。
喜欢:
RewriteRule ^/(.*)$ cgi-bin/router.py?request= [NC]
Also, I find it best to use the RewriteBasedirective when testing. Then you can just alter that instead of having to mess with the regular-expressions themselves.
另外,我发现最好RewriteBase在测试时使用该指令。然后你可以改变它,而不必弄乱正则表达式本身。
RewriteBase /test/
RewriteRule ^(.*)$ cgi-bin/router.py?request= [NC]
And, when moved to the real-world:
而且,当转移到现实世界时:
RewriteBase /
RewriteRule ^(.*)$ cgi-bin/router.py?request= [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 ScriptAliasto redirect requests for /cgi-bin/*to another directory. In my case a directory named the same in the Apache installation directory.
编辑
经过更多测试,我发现问题可能不在于表达式本身,而在于您的目录名:cgi-bin/. 默认情况下,Apache 将此目录用于 CGI 脚本,但用于ScriptAlias将请求重定向/cgi-bin/*到另一个目录。在我的例子中,Apache 安装目录中有一个名为相同的目录。
Is this a directory you created yourself, or are you using the directory Apache has aliased this to?
You have to either
这是您自己创建的目录,还是您正在使用 Apache 为其别名的目录?
你必须要么
- Use the directory Apache specifies in the configuration
- Edit the configuration to allow you to specified this yourself, or
- Choose another directory name.
- 使用Apache在配置中指定的目录
- 编辑配置以允许您自己指定,或
- 选择另一个目录名称。
Personally, I would go with #3. I tried this, and it worked fine for me.
就个人而言,我会选择#3。我试过这个,对我来说效果很好。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !bin/
RewriteRule ^(.+) bin/request.php?ctrl= [NC,L]
</IfModule>

