2011-06-12 68 views
1

我有一个.htaccess文件这段代码:不同的结果将在.htaccess代码时相比httpd.conf中

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?pic=$1 

这不正是我想要它做的。如果我访问mydomain.com/345这将是一样的,如果我会打字mydomain.com/index.php?pic=345和的print_r($ _ GET)返回此:

Array ([pic] => 345) 

然而,当我试图把这个确切的代码在我的预编译的conf文件,而不是一个.htaccess文件,它返回完整的内部文件路径,而不是,包括$ _GET参数,因为这样的:

Array ([pic] => var/www/mydomain.com/htdocs/345) 

当然,我只能获取该字符串的最后一部分,或者使用REQUEST_URI来获取数据,但这并不能解决问题,只能绕过它。任何人都知道它为什么这样做?我猜它与它在内部编译路径的事实有关?

这里是完整的配置文件,如果你需要它,我只是将域名切换到保持中立。 :-)

<VirtualHost *:80> 
    ServerAdmin [email protected] 
    ServerName mydomain.com 

    # Indexes + Directory Root 
    DirectoryIndex index.php 
    DocumentRoot /var/www/mydomain.com/htdocs/ 

    # CGI Directory 
    ScriptAlias /cgi-bin/ /var/www/mydomain.com/cgi-bin/ 
    <Location /cgi-bin> 
      Options +ExecCGI 
    </Location> 

    # Logfiles 
    ErrorLog /var/www/mydomain.com/logs/error.log 
    CustomLog /var/www/mydomain.com/logs/access.log combined 

    <Location /> 
      RewriteEngine On 
      RewriteCond %{REQUEST_FILENAME} !-f 
      RewriteCond %{REQUEST_FILENAME} !-d 
      RewriteRule ^(.*)$ index.php?pic=$1 
    </Location> 
</VirtualHost> 

编辑

我通过更换 “解决” 它:

<Location /> 

与此:

<Directory /var/www/mydomain.com/htdocs> 

不过,我还是很好奇为什么<位置/>没有工作,任何人有想法?

+0

如果您期待整数值,请使用'\ d'而不是'''。 – Gumbo 2011-06-12 14:50:22

+0

我期待着字符和数字,345只是一个示例值。不过谢谢。 – Felthragar 2011-06-12 14:52:16

回答

1

latest (apache 2.2) documentation of mod_rewrite现在明确指出应避免位置指令(搜索页面中的位置)。你可以在the users httpd mailing list上看到一些有趣的邮件。但是这并没有说明,例如in the 2.0 documentation

尽管重写规则在句法上在章节中是允许的,但这不应该是必需的,并且不受支持。

因此,看起来你是在一个神秘的不可预知的方式运行mod重写,这当然是一个可怕的地方住。感谢那个问题,现在我意识到:-)

+0

嗯是的,当我四处寻找解决方案时,我确实看到了那部分。你认为我目前的解决方案比更好吗,还是应该去做其他事情? – Felthragar 2011-06-12 15:36:44

+0

是目录是目录中的.htaccess的官方等同物。在你的定位案例中,rewriteBase指令可能会解决这个问题(告诉mod-rewrite应该从路径中删除哪个目录前缀,阅读rewriteBase文档,你会得到有关位置指令发生情况的良好提示)。 – regilero 2011-06-12 16:12:43

+0

好的,谢谢你清理那个。 – Felthragar 2011-06-14 20:01:43

相关问题