关于Apache的Rewrite的一点研究

背景

最近几天想把Mac上的Apache用起来,之前一直用的是Nginx,其对泛解析支持的非常方便友好。但是Apache的之前没有配置过,折腾了几天总算比较完美的解决了!

首先先解决在URL中去掉index.php,之前用的是.htaccess文件,里面的配置是网上找的,如下:

<Files .*>
        Order Deny,Allow
        Deny From All
</Files>
Options FollowSymLinks
RewriteEngine On
#RewriteBase /
RewriteRule ^(?:includes|sql)\b.* index.php/$0 [L]
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|jpg|png|js|css|swf|php|ico|txt|pdf|map|xml|htm?)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule (.*) index.php/$1 [PT]

这个本来没什么问题,但是我把他写到httpd.conf配置里,就老是不成功。然后就想着看看错误日志是哪里出错了。

百度了一下怎么开启Apache的rewrite日志,大都是这样的:

RewriteLog "/myfolder/mylogfile.log"
RewriteLogLevel 9

但是Apache起不来,看日志是这样的

Syntax error on line 162 of /Applications/XAMPP/xamppfiles/etc/httpd.conf:

Invalid command 'RewriteLog', perhaps misspelled or defined by a module not included in the server configuration


于是又看了看手册去:http://httpd.apache.org/docs/current/mod/mod_rewrite.html

原来是这样设置的:LogLevel rewrite:trace3,日志去看ErrorLog所指向的文件。

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/Applications/XAMPP/xamppfiles/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    LogLevel rewrite:trace3
    ErrorLog "logs/dummy-host.example.com-error_log"
    CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>


日志是这样的:

[Mon Jul 25 11:25:13.057665 2016] [rewrite:trace2] [pid 4965] mod_rewrite.c(476): [client 127.0.0.1:57703] 127.0.0.1 - - [api.mac.io/sid#7ff57b83cc00][rid#7ff57b0468a0/subreq] rewrite '/index.php/admin' -> '/index.php//index.php/admin'

[Mon Jul 25 11:25:13.057672 2016] [rewrite:trace2] [pid 4965] mod_rewrite.c(476): [client 127.0.0.1:57703] 127.0.0.1 - - [api.mac.io/sid#7ff57b83cc00][rid#7ff57b0468a0/subreq] forcing '/index.php//index.php/admin' to get passed through to next API URI-to-filename handler

[Mon Jul 25 11:25:20.980288 2016] [rewrite:trace2] [pid 4969] mod_rewrite.c(476): [client 127.0.0.1:57704] 127.0.0.1 - - [api.mac.io/sid#7ff57b83cc00][rid#7ff57a833ea0/initial] init rewrite engine with requested uri /admin


通过日志大致可以判断出,是(/index.php//index.php/admin)这里的规则写错了,遂改之如下:

<VirtualHost *:80>
DocumentRoot /home/www/lookapi
ServerName api.mac.io

<IfModule mod_rewrite.c>
        <Files .*>
                Order Deny,Allow
                Deny From All
        </Files>
        Options FollowSymLinks
        RewriteEngine On
        #RewriteBase /
        RewriteRule ^(?:includes|sql)\b.* index.php/$0 [L]
        RewriteCond %{REQUEST_URI} ^.+$
        RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|jpg|png|js|css|swf|php|ico|txt|pdf|map|xml|htm?)$ [OR]
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        RewriteCond %{REQUEST_FILENAME} -l
        RewriteRule ^ - [L]
        RewriteRule (.*) /index.php$1 [PT]
</IfModule>

<IfModule mod_deflate.c>
        DeflateCompressionLevel 7
        AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
        AddOutputFilter DEFLATE css js html htm gif jpg png bmp php
</IfModule>
</VirtualHost>

当然,一般将规则写在.htacess里比较好,这样可以比较灵活。我是因为自己开发用,再加上想配置一个泛解析,不想在每个项目里都写一个.htacess,所以偷懒想一劳永逸。

晚上补上Apache的泛解析配置方式!


泛解析配置如下:

<VirtualHost *:80>
#DocumentRoot /home/www
UseCanonicalName Off
VirtualDocumentRoot /home/www/%1.0
ServerName local.cn
ServerAlias *.local.cn

<IfModule mod_rewrite.c>
        <Files .*>
                Order Deny,Allow
                Deny From All
        </Files>
        Options FollowSymLinks
        RewriteEngine On
        #RewriteBase /
        RewriteRule ^(?:includes|sql)\b.* index.php/$0 [L]
        RewriteCond %{REQUEST_URI} ^.+$
        RewriteCond %{REQUEST_FILENAME} \.(less|gif|jpe?g|jpg|png|js|css|swf|php|ico|txt|pdf|map|xml|htm?)$ [OR]
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d [OR]
        RewriteCond %{REQUEST_FILENAME} -l
        RewriteRule ^ - [L]
        RewriteRule (.*) /index.php$1 [PT]
</IfModule>

#php_admin_value open_basedir /home/www/web/demo:/tmp:/home/www/web/common
<IfModule mod_deflate.c>
        DeflateCompressionLevel 7
        AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php
        AddOutputFilter DEFLATE css js html htm gif jpg png bmp php
</IfModule>
</VirtualHost>

这样,每次请求都会都会定位到项目目录上了,如

test.local.cn会定位到/home/www/test/

app.local.cn会定位到/home/www/app/

如此,每次添加一个项目,只需要在/home/www/下建立对应到目录,在做一个hosts就ok!

对了,上面到配置要开启Apache的mod_rewrite和mod_vhost_alias模块!


标签: apache, rewrite, 泛解析

添加新评论