3

I had an apache server where I was running my php application. It had these htaccess rules:

php_flag log_errors Off
RewriteEngine on
RewriteRule thankyou.html /thankyou.php
RewriteRule ^([^/]*)\.html$ /user.php?username=$1 [L]


RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Now I'm migrating to an nginx server and I need to make possible to see the thankyou.php and user.php page as .html pages in the url as it was on the apache server (for user.php especially it has to transform in [name-of-the-user].html name).

This is the nginx file:

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mydomain.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mydomain.com;
    root /home/forge/mydomain.com;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/mydomain.com/480288/server.crt;
    ssl_certificate_key /etc/nginx/ssl/mydomain.com/480288/server.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers MY-SSL-CIPHER;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/mydomain.com/server/*;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    access_log off;
    error_log  /var/log/nginx/mydomain.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/mydomain.com/after/*;

I tried to add

rewrite thankyou.html /thankyou.php;
rewrite ^/([^/]*)\.html$ /user.php?username=$1 break;

in different part of the nginx configuration file (for instance under location /, outside, under location /thankyou.php) but it doesn't work properly.

Giuseppe87
  • 406
  • 4
  • 14
  • 1
    You shouldn't use `break` as the `.php` URI needs to be processed in another `location` block. Try using `last` instead. – Richard Smith Jan 26 '19 at 16:43
  • @RichardSmith thanks for the answer. I just solved. If you wanna put your comment as an answer I can flag it as the solution. – Giuseppe87 Jan 26 '19 at 17:43

1 Answers1

1

The URI is internally rewritten from foo.html to foo.php.

The rewrite...break statement, terminates the rewrite engine and proceeds to process the new URI within the same location block.

However, foo.php needs to be processed in the location \.php$ block.

The rewrite...last statement, terminates the rewrite engine and then restarts the search for a location to process the new URI.

For example:

rewrite ^/([^/]*)\.html$ /user.php?username=$1 last;

See this document for details.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81