重写规则不适用于虚拟主机

时间:2016-02-14 20:20:44

标签: apache .htaccess mod-rewrite

我在以下问题上花了2天时间:

我在apache 2.4.7服务器上有一个虚拟主机,配置如下:

<VirtualHost *:80>
        DocumentRoot /home/xxx/test/www
        ServerAdmin webmaster@xxx.com
        ServerName test.xxx.com
        ServerAlias www.test.xxx.com
        AddHandler x-httpd-php5 .php

        ErrorLog ${APACHE_LOG_DIR}/xxx-test-error.log
        LogLevel warn
        LogLevel alert rewrite:trace6

        CustomLog ${APACHE_LOG_DIR}/xxx-test-access.log combined

        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>

        <Directory /home/xxx/test/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
        </Directory>

</VirtualHost>

这样的.htaccess文件:

 <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^article/([^/\.]+)/?$ article-page.php?name=$1 [L]
    RewriteRule ^list/.?$ list.php [L]
    RewriteRule ^sitemap.xml$ sitemap.php [L]
    </IfModule>

我希望使用list.php

启动/ list /处理所有URL

但是当我浏览网址时:http://test.xxx.com/list/yyy-category

它只返回404.我已经检查过,list.php存在且拥有良好的权利。

我检查了日志,我得到了:

[Sun Feb 14 21:00:23.764091 2016] [rewrite:trace3] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxx.com/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] add path info postfix: /home/xxx/test/www/list.php -> /home/xxx/test/www/list.php/yyy-category, referer: http://test.xxx.com/
[Sun Feb 14 21:00:23.764163 2016] [rewrite:trace3] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxx.com/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] strip per-dir prefix: /home/xxx/test/www/list.php/yyy-category -> list.php/yyy-category, referer: http://test.xxx.com/
[Sun Feb 14 21:00:23.764194 2016] [rewrite:trace3] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxx.com/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] applying pattern '^article/([^/\\.]+)/?$' to uri 'list.php/yyy-category', referer: http://test.xxx.com/
[Sun Feb 14 21:00:23.764228 2016] [rewrite:trace3] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxx.com/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] add path info postfix: /home/xxx/test/www/list.php -> /home/xxx/test/www/list.php/yyy-category, referer: http://test.xxx.com/
[Sun Feb 14 21:00:23.764259 2016] [rewrite:trace3] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxx.com/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] strip per-dir prefix: /home/xxx/test/www/list.php/yyy-category -> list.php/yyy-category, referer: http://test.xxx.com/
[Sun Feb 14 21:00:23.764287 2016] [rewrite:trace3] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxx.com/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] applying pattern '^list/(.?)$' to uri 'list.php/yyy-category', referer: http://test.xxx.com/
[Sun Feb 14 21:00:23.764355 2016] [rewrite:trace3] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxxcom/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] add path info postfix: /home/xxx/test/www/list.php -> /home/xxx/test/www/list.php/yyy-category, referer: http://test.xxx.com/
[Sun Feb 14 21:00:23.764386 2016] [rewrite:trace3] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxx.com/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] strip per-dir prefix: /home/xxx/test/www/list.php/yyy-category -> list.php/yyy-category, referer: http://test.xxx.com/
[Sun Feb 14 21:00:23.764414 2016] [rewrite:trace3] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxx.com/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] applying pattern '^sitemap.xml$' to uri 'list.php/yyy-category', referer: http://test.xxx.com/
[Sun Feb 14 21:00:23.764445 2016] [rewrite:trace1] [pid 17393] mod_rewrite.c(468): [client 79.81.246.191:53329] 79.81.246.191 - - [test.xxx.com/sid#66dd81c14970][rid#66dd81b400a0/subreq] [perdir /home/xxx/test/www/] pass through /home/yyy/test/www/list.php, referer: http://test.xxx.com/

有关如何解决这个问题的想法吗?

2 个答案:

答案 0 :(得分:1)

你的.htaccess匹配以&#34;列表/&#34;开头的网址。然后是一个字符,或者没有字符(那是什么。?的意思)。

在您的情况下,您必须改变规则:

RewriteRule ^list/.*$ list.php [L]

。* 符号表示&#34;我想要任何字符,0次或更多次&#34;

答案 1 :(得分:0)

我找到了它,事实上它真是愚蠢。如日志中所示,它会尝试使其与以下文件匹配,并且它不是一条好路径

RewriteRule ^list/(.+)?$ list-page.php?query=$1 [L]

不要混淆apache的最好方法是不要让文件的名称与URL前缀不同,例如&#34; list-page.php&#34;。现在有效的规则是:

configurations {
    jibx
}

dependencies {
    jibx group: 'org.jibx', name: 'jibx-tools', version: '1.2.6'
    jibx group: 'org.jibx', name: 'jibx-bind', version: '1.2.6'
    jibx group :'org.jibx', name: 'jibx-extras', version:'1.2.6'
}


task generate() {
    ant.java(classname: 'org.jibx.binding.Compile', fork: true, classpath: "${configurations.jibx.asPath}") {
        arg(line: "-p 'com.cigna.ccmi'")
        arg(line: "-t 'src/main/java'")
    }
}

def compileJibx() {
    ant {
        taskdef(name: 'bind',
            classname: 'org.jibx.binding.ant.CompileTask',
            classpath: configurations.jibx.asPath)

        **bind(binding: "${projectDir}/src/main/resources/jibx/common-config-JibX.xml", verbose: 'false')** {
            classpath {
                pathelement(path: "${projectDir}/build/classes/main")
            }
        }
    }   
}

compileJava.dependsOn generate
compileJava.doLast {
    compileJibx()
}