redirect工作但不是rewriterule(mod_rewrite已加载)

时间:2014-12-19 16:50:29

标签: apache .htaccess mod-rewrite

重定向工作

Alias /www /var/www
<Directory /var/www>
        Options +FollowSymLinks
        DirectoryIndex index.php
<IfModule mod_rewrite.c>
        RedirectMatch /www/php/(.*) http://www.google.com/usingredirection/$1
</IfModule>


=> http://myserver/www/php/foo redirect to http://www.google.com/usingredirection/foo

所以:加载了Mod_rewrite(因为IfModule)和mod_alias

但RewriteRule不起作用

<Directory /var/www>
        Options +FollowSymLinks
        DirectoryIndex index.php
<IfModule mod_rewrite.c>
        RewriteEngine on
        Rewritebase /var/www
        RewriteRule ^/www/php/(.*)$ http://www.google.com/rule1/$1 [R,L]
        RewriteRule ^(.*)$ http://www.google.com/rule2/$1 [R,L]
</IfModule>

apache日志说/ var / www / php / foo不存在(这是真的) 但为什么不适用rewriterule?

所有这些都在* .conf文件中。 我试图在一个htacess中移动重写规则但是一旦我添加了一个重写引擎就会一直抱怨 选项FollowSymLinks或SymLinksIfOwnerMatch已关闭,这意味着即使FollowSymLinks已启用,也禁止使用RewriteRule指令

所以现在我只尝试使用rewriterule模仿重定向

我的最终目标

/ www / php / * .php像往常一样提供php(和现有文件)

/ www / php / preview / * =&gt; /www/php/preview.php?path=$1

/ www / php / * =&gt; /www/php/index.php?path=$1

编辑:在第一次评论之后,我觉得发布整个内容可能有帮助

所以这是httpd.conf。 这个文件不是我的。它是readyNAS Duo的标准配置。 我刚刚在我自己的conf中添加了一个include。

的httpd.conf

ServerRoot /frontview/ui/resource/html

...

Listen 80

# Please keep this LoadModule: line here, it is needed for installation.
LoadModule perl_module /usr/lib/apache2/modules/mod_perl.so
LoadModule auth_pam_module /usr/lib/apache2/modules/mod_auth_pam.so
LoadModule auth_sys_group_module /usr/lib/apache2/modules/mod_auth_sys_group.so
LoadModule apreq_module /usr/lib/apache2/modules/mod_apreq2.so
APREQ2_ReadLimit 2147483648
LoadModule php5_module /usr/lib/apache2/modules/libphp5.so
AddType application/x-httpd-php .php
PHPIniDir /etc/php5/apache2

ExtendedStatus On

User admin
Group admin

ServerAdmin admin@localhost

Options All Indexes
DocumentRoot /frontview/ui/resource/html

<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
</Files>

<Files ~ "\.(js|jpg|jpeg|gif|html|htm|png|css)">
    Header set Pragma: public
</Files>

UseCanonicalName Off
TypesConfig /etc/mime.types
DefaultType text/plain

<IfModule mod_mime_magic.c>
    MIMEMagicFile share/magic
</IfModule>

HostnameLookups Off

...

<IfModule mod_autoindex.c>
    IndexOptions Charset=
    IndexOptions FancyIndexing NameWidth=*
    AddIconByEncoding (CMP,/images/Icons/compressed.gif) x-compress x-gzip
    ...more icons...
    AddIcon /images/Pix.gif ^^BLANKICON^^
    DefaultIcon /images/Icons/unknown.gif
    ReadmeName /html/AUTOINDEX.html
    HeaderName HEADER.html
    IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t
</IfModule>

<IfModule mod_mime.c>
    AddEncoding x-compress Z
    ...more mime...
    AddType video/x-ms-wvx .wvx

    AddHandler cgi-script .cgi .sh .pl
</IfModule>

#AddDefaultCharset utf-8

<IfModule mod_setenvif.c>
    BrowserMatch "Mozilla/2" nokeepalive
    ...more browser...
    # Drop the Range header when more than 5 ranges.
    # CVE-2011-3192
    SetEnvIf Range (,.*?){5,} bad-range=1
    RequestHeader unset Range env=bad-range
</IfModule>

DAVLockDB /ramfs/WebDAVLock.db
DAVMinTimeout 600

Listen 443
SSLEngine On
SSL stuff ...

# -------------------------------------------------------------------
# Uncomment following when more graceful error message docs are used.
# Currently IE defaults it it's browser message, overriding any error
# docs we create anyway.
# -------------------------------------------------------------------
# ErrorDocument 401 /error/401.html
# ErrorDocument 403 /error/403.html
# ErrorDocument 404 /error/404.html
# ErrorDocument 500 /error/500.html
# Alias /error /frontview/ui/error

<Location />
  Options ExecCGI
  DirectoryIndex redirect.html index.html index.htm index.php
</Location>

...

<Location /index.html>
  SetHandler server-status
  Order Deny,Allow
  Deny from all
</Location> 

Alias /images/     /frontview/ui/resource/images/
...

PerlSwitches -I /etc/frontview/addons/ui
Alias /addons/ /etc/frontview/addons/ui/

Alias /admin /frontview/ui/resource/html
<Location /admin>
    DirectoryIndex index.html
    Options ExecCGI
    AuthType Basic
    AuthName "Control Panel"
    require user admin
</Location>

Alias get_handler /frontview/lib
PerlSwitches -I /frontview/lib
PerlModule Frontview
PerlModule get_handler
<Location /get_handler>
    SetHandler perl-script
    PerlHandler get_handler
    PerlSendHeader On
    Options ExecCGI
    Order allow,deny
    Allow from all
        AuthType Basic
        AuthName "Control Panel"
        require user admin
</Location>

Alias /np_handler /frontview/lib
<Location /np_handler>
    Options ExecCGI
        DirectoryIndex np_handler.pl 
    Allow from all
</Location>

LoadModule listhandler_module /frontview/lib/mod_listhandler.so
<Location /list_handler>
  SetHandler listhandler
</Location>

Alias /dir_list /frontview/lib/dir_list.pl
<Location /dir_list>
        AuthType Basic
        AuthName "Control Panel"
        require user admin
    Options ExecCGI
    Allow from all
</Location>

...

the file ends with Include /etc/frontview/apache/Virtual.conf
which is
<VirtualHost _default_:80>
  SSLEngine off
  RewriteEngine on
  RewriteRule ^/admin$ https://%{SERVER_NAME}/admin
  RewriteRule ^/admin/(.*)$ https://%{SERVER_NAME}/admin/$1 [R,L]
</VirtualHost>

然后httpd.conf以我的文件

的包含结束
Alias /www /var/www
<Directory /var/www>
        Options +FollowSymLinks
        DirectoryIndex index.php


        #redirecting works
        #RedirectMatch /www/php/(.*) http://www.google.com/redirecting/$1

        RewriteEngine on
        #rewriterule dont work
        RewriteRule ^(.*)$ http://www.google.com/rewriting/$1 [R,L]
</Directory>

我删除了Rewritebase,这应该是不必要的。 我删除了1条规则以使事情更加清晰。 现在我预计这会捕获所有服务器/ www / php / *并将它们重定向到Google但它仍然无法正常工作

1 个答案:

答案 0 :(得分:2)

由于/var/www是您的DocumentRoot,因此您不得在重写规则中使用它们。请在Apache配置中使用这些规则:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^(.+?\.php)$ http://www.google.com/rule1/$1 [R,L]
    RewriteRule ^(.*)$ http://www.google.com/rule2/$1 [R,L]
</IfModule>