Nginx重写index.php?page =联系人只需联系

时间:2016-10-10 13:09:21

标签: nginx

我遇到了麻烦。我希望从" index.php?page = somepage"重写URL。写入" somepage"。

这就是.htaccess如果有帮助的话

RewriteEngine on
RewriteRule ^(\w+)$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(\w+)+\/$ index.php?page=$1 [L,NC,QSA]

服务器阻止:

server {
        listen 80;
        listen [::]:80;
        server_name domain.com;

        root /home/www/domain.com;

        index index.php index.html index.htm;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                try_files $uri =404;
                rewrite ^/(w+)$ /index.php?page=$1 last;
                rewrite ^/(w+)+/$ /index.php?page=$1 last;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        }
}

2 个答案:

答案 0 :(得分:1)

请尝试以下代码,

server {
   ...
   rewrite ^/index.php?page=(.*) /$1 permanent;
   ...
   location ~ /(.*) {
      try_files $uri $uri/ /index.php?page=$1;
   }

   location ~ \.php {
      ...
      try_files $uri =404;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include fastcgi_params;
      ...
   }
   ...
}

答案 1 :(得分:0)

尝试使用.htaccess到nginx转换器,这段代码似乎有效:

rewrite ^/(w+)$ /index.php?page=$1 last;
rewrite ^/(w+)+/$ /index.php?page=$1 last;

编辑:我通过考虑重写来使事情过于复杂。我试图做的解决方案是:

location / {
    try_files $uri $uri/ /index.php?page=$uri;
}