2

How would I convert the following Apache rule

RewriteRule ^watch/([A-Za-z0-9-]+)/?$ watch.php?v=$1 [NC]

into the appropriate format for an NGINX configuration file?

Further edit:

server {
    listen 80;
    listen [::]:80;

    server_name domain.tld www.domain.tld;
    root /var/www/ROOT;
    index index.php index.html index.htm;

        return 302 https://www.domain.tld$request_uri;

    location / {
        #
        # HIDE PHP EXTENSION
        #
        try_files $uri $uri/ @extensionless-php;
    }


    location /watch.php {
        rewrite ^/watch/([A-Za-z0-9-]+)/?$ /watch.php?v=$1;
    }

    #
    # HIDE PHP EXTENSION
    #
    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }

    #
    # BLOCK REFERRER URLS
    #

    location ~ \.php$ {
        try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    error_page 401 /error_pages/401.php;
    error_page 403 /error_pages/403.php;
    error_page 404 /error_pages/404.php;
    error_page 405 /error_pages/405.php;
    error_page 408 /error_pages/408.php;

    location ^~ /error/ {
        internal;
        root /error_pages/;
    }
  • Edit your question and add the nginx server configuration block. – Richard Smith Dec 28 '15 at 17:20
  • You have a return 302 which should not be there. And the location /watch.php should be location /watch. And you should probably stick a last modifier on the end of the rewrite. – Richard Smith Dec 28 '15 at 17:29
  • What is the recommended way to force HTTPS, I know in Apache it can be done with the following RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR] RewriteCond %{HTTPS} off RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302] RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* %{HTTP_HOST}%{REQUEST_URI} [L,R=302] – itteam Dec 28 '15 at 17:31
  • Now I can access the page at domain-name.tld/watch/1 but not domain-name.tld/watch?v=1, I would like to have it accessible via both if it is possible – itteam Dec 28 '15 at 17:35
  • Use two separate server blocks see this – Richard Smith Dec 28 '15 at 17:36
2

The URI in nginx includes the leading /, so the equivalent rewrite would be:

location /watch/ {
    rewrite ^/watch/([A-Za-z0-9-]+)/?$ /watch.php?v=$1 last;
}

Would that go in the location / { } block?

That is a slightly more complicated question, and it depends on the nginx configuration, particularly what other location blocks exist.

The options are:

  • Place it near the top of the server container (outside a location block)
  • Place it in the / location
  • Place it in a /watch location
  • Would that go in the location / { } block? Because when I try it and browse to the page it just downloads the page into my downloads folder with no file extension – itteam Dec 28 '15 at 17:00
  • @itteam Does your configuration support PHP files? – Richard Smith Dec 28 '15 at 17:16
  • @itteam I just realised that the other place to stick it is above the rewrite in the @extensionless-php location block (as long as they all have a last modifier). – Richard Smith Dec 28 '15 at 22:16
  • Ok, I will try that, I don't think there would be much difference between Apache & NGINX for me anyway as my pages only use simple PHP includes for items like the footer and navigation bars – itteam Dec 29 '15 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.