首页 » Apache » 正文

记一次web页面的rewrite

Apache:2.4.x
nginx:1.16.1
以meiupic(美优相册) 3.0为例,nginx采用rewrite规则,配置后重新加载配置文件即可生效
###nginx rewrite规则
location / {
    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php?q=$1&$args last;
        break;
    }
}
apache2需要rewrite模块
[root@Linux httpd]# httpd -M|grep rewrite
rewrite_module (shared)
更改httpd.conf的配置,使其能读取.htaccess的内容
[root@Linux httpd]# cat /etc/httpd/conf/httpd.conf|grep AllowOverride
    AllowOverride none
    AllowOverride None
    # AllowOverride controls what directives may be placed in .htaccess files.
    AllowOverride None
    AllowOverride None
从配置文件中查看可以知道每处AllowOverride None所在的位置分别为
1、
106 <Directory />
107     AllowOverride None
108     Require all denied
109 </Directory>
2、
128 <Directory "/var/www">
129     AllowOverride None
130     # Allow open access:
131     Require all granted
132 </Directory>
3、
151     # AllowOverride controls what directives may be placed in .htaccess files.
152     # It can be "All", "None", or any combination of the keywords:
153     #   Options FileInfo AuthConfig Limit
154     #
155     AllowOverride None
4、
260 <Directory "/var/www/cgi-bin">
261     AllowOverride None
262     Options None
263     Require all granted
264 </Directory>
从文件内容描述来看,第三处为.htaccess文件的读取控制,将None修改为ALL或者FileInfo,如果不生效就把所有的AllowOverride None的None都更改为ALL或者FileInfo,重启httpd后生效。
[root@Linux httpd]# cat /var/www/meiupic/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteRule ^ index.php?q=%1&%{QUERY_STRING} [QSA,L]
</IfModule>

发表评论