伪静态 Rewrite 教程 Nginx、Apache 规则对照,优化 URL 利于收录

PHP 站点伪静态 Rewrite 配置分享,Nginx、Apache 两套可直接复制的伪静态规则,美化 URL 地址,去掉动态参数,更利于搜索引擎收录与 SEO 优化。

Nginx rewrite 设置

server{
... 省略
    location / {
            index  index.php index.html index.htm;
             #如果请求既不是一个文件,也不是一个目录,则执行一下重写规则             if (!-e $request_filename)
             {
                #地址作为将参数rewrite到index.php上。
                rewrite ^/(.*)$ /index.php?s=$1;
                #若是子目录则使用下面这句,将subdir改成目录名称即可。
                #rewrite ^/subdir/(.*)$ /subdir/index.php?s=$1;
             }
        }
        
     ... 省略}


Apache rewrite 设置

1. httpd.conf配置文件中加载了mod_rewrite.so模块

# LoadModule rewrite_module path/mod_rewrite.so

取消注释

LoadModule rewrite_module path/mod_rewrite.so

2. AllowOverride None 将None改为 All

<Directory />
...    
AllowOverride All 
...
</Directory>

3. 重启 apache


Apache下如果.htaccess 为空 则加入

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>