Nginx重写无法正常工作

时间:2018-08-02 08:46:54

标签: nginx

我想将/ author / xxx改写到/index.php?author_name=xxx

rewrite ^/author/(.*)/$ /index.php?author_name=$1;

当我应用代码时,浏览器网址如下所示:

http://www.site-name.com/?author_name=xxx

我希望网址看起来像:

http://www.site-name.com/author/xxx,但它将重写index.php?author_name = xxx

有什么想法要解决吗?

我的Nginx conf文件在下面:

server {
        server_name site-name.com;
        rewrite ^(.*) http://www.site-name.com$1 permanent;
        listen   8080;
        }
server {
        listen   8080;
        access_log off;
        # access_log   /home/www.site-name.com/logs/access_log;
        error_log on;
        # error_log /home/www.site-name.com/logs/error.log; 
        #add_header X-Frame-Options SAMEORIGIN;
        #add_header X-Frame-Options "ALLOW-FROM https://www.site-name.xyz";
        add_header X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        root /home/www.site-name.com/public_html;
        include /etc/nginx/conf/ddos2.conf;
        # include /etc/nginx/conf/cors.conf;
        index index.php index.html index.htm;
        server_name www.site-name.com;



location / {
try_files $uri $uri/ /index.php?q=$uri&$args;

}


rewrite ^/author/(.*)/$ /index.php?author_name=$1;




        location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include /etc/nginx/fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
            #fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_index index.php;
        fastcgi_connect_timeout 250;
        fastcgi_send_timeout 250;
        fastcgi_read_timeout 250;
        fastcgi_buffer_size 256k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors on;
            fastcgi_param SCRIPT_FILENAME /home/www.site-name.com/public_html$fastcgi_script_name;
        }

include /etc/nginx/conf/drop.conf;

    }

1 个答案:

答案 0 :(得分:0)

您的rewrite看起来不错:在extern中,您可以使用/author/...,而在内部它将使用index.php?...

我想您还想将index.php?...重定向到该路径:

rewrite ^/author/([^/]*)/?$ /index.php?author_name=$1 break;
rewrite ^/index.php$ https://www.example.com/author/$arg_author_name/ redirect;
root /var/www/html/;
fastcgi_index index.php;
fastcgi_pass php;
include fastcgi_params;