Question

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;

    }
}
Was it helpful?

Solution 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;

OTHER TIPS

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

I think you put the rules swapped, try something like

rewrite ^/(?:profiles|id)/(.*) /profiles?id=$1 last;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top