Не удается переименовать определенный файл .php с именами .html в nginx - PullRequest
0 голосов
/ 26 января 2019

У меня был сервер Apache, на котором я запускал свое приложение php. У него были следующие правила htaccess:

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]

Теперь я выполняю миграцию на сервер nginx, и мне нужно, чтобы страницы thankyou.php и user.php отображались в виде страниц .html в URL, как это было на сервере apache (особенно для user.php должен преобразоваться в [имя пользователя] .html name).

Это файл nginx:

# 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/*;

Я пытался добавить

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

в другой части файла конфигурации nginx (например, в папке /, снаружи, в папке /thankyou.php), но он не работает должным образом.

1 Ответ

0 голосов
/ 26 января 2019

URI внутренне перезаписывается с foo.html до foo.php.

Оператор rewrite...break завершает работу механизма перезаписи и приступает к обработке нового URI в того же location block.

Однако foo.php необходимо обработать в блоке location \.php$.

Оператор rewrite...last завершает механизм перезаписи и затем возобновляет поиск location для обработки нового URI.

Например:

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

Подробнее см. в этом документе .

...