How to use Nginx to separate variables with slashes

北城以北 提交于 2020-01-16 03:40:09

问题


I'd like to be able to use

www.example.com/profiles/1234567890

instead of

www.example.com/profiles?id=1234567890

but was unable to figure out how to do so.

I found an apache equivalent here: Turn text after slashes into variables with HTACCESS but I don't know how to get it to work in Nginx. I only need the one variable which is id.

Additionally, is it possible to do a rewrite such that

www.example.com/id/1234567890

points to

www.example.com/profiles/1234567890

but the url doesn't change?

Nginx config

server {
    server_name www.domain.com;
    rewrite ^(.*) http://domain.com$1 permanent;
}

server {
    listen 80;
    server_name domain.com;
    root /var/www/domain.com/public;
    index index.php;

    access_log  /var/www/domain.com/access.log;
    error_log  /var/www/domain.com/error.log;

    rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
    rewrite ^/id/(.*)$ /profiles/$1 last;

    # unless the request is for a valid file, send to bootstrap
    if (!-e $request_filename)
    {
        #rewrite ^(.+)$ /index.php?q=domain.com last;
        rewrite ^(.*)$ $1.php last;
    }

    # catch all
    error_page 404 /index.php;

    # Directives to send expires headers and turn off 404 error logging.
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
        access_log off;
    }

    location /phpmyadmin {
        root /usr/share/;
        index index.php index.html index.htm;
        location ~ ^/phpmyadmin/(.+\.php)$ {
               try_files $uri =404;
               root /usr/share/;
               fastcgi_pass 127.0.0.1:9000;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include /etc/nginx/fastcgi_params;
        }
        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
               root /usr/share/;
        }
    }
    location /phpMyAdmin {
        rewrite ^/* /phpmyadmin last;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    ## Disable viewing .htaccess & .htpassword
    location ~ /\.ht {
        deny  all;
    }

    location / {
                # This is cool because no php is touched for static content. 
                # include the "?$args" part so non-default permalinks doesn't break when using query string                
                try_files $uri $uri/ @extensionless-php?$args;    
        }

    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }

    # use fastcgi for all php files
    location ~ \.php$
    {
        try_files $uri =404;

        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;

        # Some default config
        fastcgi_connect_timeout        20;
        fastcgi_send_timeout          180;
        fastcgi_read_timeout          180;
        fastcgi_buffer_size          128k;
        fastcgi_buffers            4 256k;
        fastcgi_busy_buffers_size    256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors    on;
        fastcgi_ignore_client_abort off;

    }
}

回答1:


Inside the server block of your configuration file, try adding this:

rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
rewrite ^/id/(.*)$ /profiles/$1 last;

Restart nginx. It should work. More information can be found here: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html




回答2:


I think you put the rules swapped, try something like

rewrite ^/(?:profiles|id)/(.*) /profiles?id=$1 last;



回答3:


For anyone looking for the answer, this worked:

rewrite ^/profiles/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;
rewrite ^/id/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;


来源:https://stackoverflow.com/questions/16255944/how-to-use-nginx-to-separate-variables-with-slashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!