如何使用Nginx来用斜线分隔variables

我想能够使用

www.example.com/profiles/1234567890

代替

www.example.com/profiles?id=1234567890

但无法弄清楚如何做到这一点。

我在这里find了一个类似于 apache的东西: 将斜线后的文本转换成HTACCESS的variables,但我不知道如何让它在Nginx中工作。 我只需要一个是idvariables。

另外,是否有可能做这样的重写

www.example.com/id/1234567890

指着

www.example.com/profiles/1234567890

但url不变?

Nginxconfiguration

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

在您的配置文件的服务器块内,尝试添加以下内容:

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

重新启动nginx。 它应该工作。 更多信息可以在这里找到: http : //nginx.org/en/docs/http/ngx_http_rewrite_module.html

我想你把规则交换,尝试类似的东西

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

对于任何寻找答案的人来说,这工作:

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