Home > OS >  How can I rewrite a request without changing the browers URL in nginx?
How can I rewrite a request without changing the browers URL in nginx?

Time:10-16

I am using nginx and my webservers htdocs folder contains several *.html files. I want to serve them even if the request doesn't contain the *.html extension.

Most answers I found here on SO apply a rewrite which will be visible in the browsers address bar. How can I achieve the same but "silently"?

Example: www.example.com/foo => www.example.com/foo.html

If I'm not mistaken, then /opt/bitnami/apps/wordpress/conf/nginx-app.conf is the best location to apply these changes.

index index.php index.html index.htm;

if ($request_uri !~ "^/phpmyadmin.*$")
{
  set $test  A;
}
if ($request_uri !~ "^/bitnami.*$")
{
  set $test  "${test}B";
}
if (!-e $request_filename)
{
  set $test  "${test}C";
}
if ($test = ABC) {
  rewrite ^/(. )$ /index.php?q=$1 last;
}

# Deny access to any files with a .php extension in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
  deny all;
}

# Disable logging for not found files and access log for the favicon and robots
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}



include "/opt/bitnami/apps/bitnami/banner/conf/banner-substitutions.conf";
include "/opt/bitnami/apps/bitnami/banner/conf/banner.conf";

# Deny all attempts to access hidden files such as .htaccess or .htpasswd.
location ~ /\. {
    deny all;
}


location ~ \.php$ {
    fastcgi_split_path_info ^(. \.php)(/. )$;
    fastcgi_read_timeout 300;
    fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME $request_filename;
    include fastcgi_params;
}

CodePudding user response:

You could use the try_files

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

document at https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

index index.php index.html index.htm;

if ($request_uri !~ "^/phpmyadmin.*$")
{
  set $test  A;
}
if ($request_uri !~ "^/bitnami.*$")
{
  set $test  "${test}B";
}
if (!-e $request_filename)
{
  set $test  "${test}C";
}
if (!-e $request_filename.html)
{
  set $test  "${test}D";
}

if ($test = ABCD) {
  rewrite ^/(. )$ /index.php?q=$1 last;
}

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

# Deny access to any files with a .php extension in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
  deny all;
}

# Disable logging for not found files and access log for the favicon and robots
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}



include "/opt/bitnami/apps/bitnami/banner/conf/banner-substitutions.conf";
include "/opt/bitnami/apps/bitnami/banner/conf/banner.conf";

# Deny all attempts to access hidden files such as .htaccess or .htpasswd.
location ~ /\. {
    deny all;
}


location ~ \.php$ {
    fastcgi_split_path_info ^(. \.php)(/. )$;
    fastcgi_read_timeout 300;
    fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME $request_filename;
    include fastcgi_params;
}
  • Related