【问题标题】:redirect works but not rewriterule (mod_rewrite loaded)重定向有效,但不重写(加载 mod_rewrite)
【发布时间】:2015-02-18 15:31:24
【问题描述】:

重定向作品

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 不存在(这是真的) 但为什么它不应用重写规则?

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

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

我的终极目标

/www/php/*.php 照常服务 php(和现有文件)

/www/php/preview/* => /www/php/preview.php?path=$1

/www/php/* => /www/php/index.php?path=$1

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

所以这里是 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 条规则以使事情更清楚。 现在我希望这可以捕获所有 server/www/php/* 并将它们重定向到 Google,但它仍然不起作用

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    由于/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>
    

    【讨论】:

    • 如果没有最终的/,我可以说Rewritebase /var/www 永远不会正确吗?
    • 是的,但这里甚至不需要 Rewritebase /var/www/,因为这里 /var/www 是 DocumentRoot
    • 是的,我也看过。非常感谢!
    • 我不确定 DocumentRoot。我试图调整readynas duo 的httpd.conf 来托管我自己的php 文件。如果可能的话,我不想碰这个 httpd.conf,所以我只是在 conf 中的最后一个包含之后添加我的文件的包含。我只留下了一条规则来重定向所有流量:RewriteRule ^(.*)$ google.com/rewriting/$1 [R,L] 但它仍然不起作用
    • 启用 .htaccess 并将此规则放在根 .htaccess 中(要启用 htaccess,您可以联系您的托管服务提供商)。
    猜你喜欢
    • 1970-01-01
    • 2017-05-08
    • 2011-09-28
    • 1970-01-01
    • 2011-07-24
    • 2021-04-10
    • 2011-04-14
    • 2011-09-08
    • 2012-02-22
    相关资源
    最近更新 更多