Nginx – Apache .htaccess Rewrite Rules Not Effective

.htaccessnginxrewrite

I'm running a website "PHP script" on aaPanel with Debian 10 installed. All works ok but the images are not showing/broken

The /image/ path is a rewrite rule, and it resolves to image.php which loads the image from the uploads folder. after converting .htaccess to Nginx and add them to the system all image paths going to

www.website.com/image/m/112/112/335757712_1152702765_706821275.jpeg

The actual path should be

www.website.com/uploads/media/112/112/335757712_1152702765_706821275.jpeg

Below are the .htaccess rules

RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*)                                       $1                                              [L]
RewriteRule ^(([^/]*)+)?$                               index.php?a=$1                                  [L]

RewriteRule ^welcome/?$                                 index.php?a=welcome                             [NC]

RewriteRule ^stream/?$                                  index.php?a=stream                              [NC]
RewriteRule ^stream/logout$                             index.php?a=stream&logout                       [NC]

RewriteRule ^explore/?$                                 index.php?a=explore                             [NC]
RewriteRule ^explore/filter/([^/]+)/?$                  index.php?a=explore&filter=$1                   [NC]
RewriteRule ^explore/popular$                           index.php?a=explore&popular                     [NC]
RewriteRule ^explore/liked$                             index.php?a=explore&liked                       [NC]

RewriteRule ^history/?$                                 index.php?a=history                             [NC]

RewriteRule ^upload/?$                                  index.php?a=upload                              [NC]

RewriteRule ^pro/?$                                     index.php?a=pro                                 [NC]

RewriteRule ^stats/?$                                   index.php?a=stats                               [NC]
RewriteRule ^stats/filter/([^/]+)/?$                    index.php?a=stats&filter=$1                     [NC]

RewriteRule ^profile/([^/]+)/?$                         index.php?a=profile&u=$1                        [NC]
RewriteRule ^profile/([^/]+)/([^/]+)/?$                 index.php?a=profile&u=$1&r=$2                   [NC]
RewriteRule ^profile/([^/]+)/filter/([^/]+)/?$          index.php?a=profile&u=$1&filter=$2              [NC]

RewriteRule ^notifications/?$                           index.php?a=notifications                       [NC]
RewriteRule ^notifications/filter/([^/]+)/?$            index.php?a=notifications&filter=$1             [NC]

RewriteRule ^settings/?$                                index.php?a=settings                            [NC]
RewriteRule ^settings/([^/]+)/?$                        index.php?a=settings&b=$1                       [NC]

RewriteRule ^messages/?$                                index.php?a=messages                            [NC]
RewriteRule ^messages/([^/]+)/([^/]+)/?$                index.php?a=messages&u=$1&id=$2                 [NC]

RewriteRule ^track/([^/]+)/?$                           index.php?a=track&id=$1                         [NC]
RewriteRule ^track/([^/]+)/edit/?$                      index.php?a=track&id=$1&type=edit               [NC]
RewriteRule ^track/([^/]+)/report/?$                    index.php?a=track&id=$1&type=report             [NC]
RewriteRule ^track/([^/]+)/stats/?$                     index.php?a=track&id=$1&type=stats              [NC]
RewriteRule ^track/([^/]+)/likes/?$                     index.php?a=track&id=$1&type=likes              [NC]
RewriteRule ^track/([^/]+)/stats/filter/([^/]+)/?$      index.php?a=track&id=$1&type=stats&filter=$2    [NC]
RewriteRule ^track/([^/]+)/([^/]+)/?$                   index.php?a=track&id=$1&name=$2                 [NC]

RewriteRule ^playlist/([^/]+)/?$                        index.php?a=playlist&id=$1                      [NC]
RewriteRule ^playlist/([^/]+)/edit/?$                   index.php?a=playlist&id=$1&edit=true            [NC]
RewriteRule ^playlist/([^/]+)/([^/]+)/?$                index.php?a=playlist&id=$1&name=$2              [NC]

RewriteRule ^search/filter/([^/]+)/([^/]+)/?$           index.php?a=search&filter=$1&q=$2               [NC]

RewriteRule ^page/([^/]+)/?$                            index.php?a=page&b=$1                           [NC]

RewriteRule ^recover/?$                                 index.php?a=recover                             [NC]
RewriteRule ^recover/do/?$                              index.php?a=recover&r=1                         [NC]

RewriteRule ^image/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$   image.php?t=$1&w=$2&h=$3&src=$4

Below are the Nginx converted rules

location / {
  if (-e $request_filename){
    rewrite ^/(.*) /$1 break;
  }
  rewrite ^/(([^/]*)+)?$ /index.php?a=$1 break;
}

location /welcome {
  rewrite ^/welcome/?$ /index.php?a=welcome;
}

location /stream {
  rewrite ^/stream/?$ /index.php?a=stream;
}

location = /stream/logout {
  rewrite ^(.*)$ /index.php?a=stream&logout;
}

location /explore {
  rewrite ^/explore/?$ /index.php?a=explore;
  rewrite ^/explore/filter/([^/]+)/?$ /index.php?a=explore&filter=$1;
}

location = /explore/popular {
  rewrite ^(.*)$ /index.php?a=explore&popular;
}

location = /explore/liked {
  rewrite ^(.*)$ /index.php?a=explore&liked;
}

location /history {
  rewrite ^/history/?$ /index.php?a=history;
}

location /upload {
  rewrite ^/upload/?$ /index.php?a=upload;
}

location /pro {
  rewrite ^/pro/?$ /index.php?a=pro;
}

location /stats {
  rewrite ^/stats/?$ /index.php?a=stats;
  rewrite ^/stats/filter/([^/]+)/?$ /index.php?a=stats&filter=$1;
}

location /profile {
  rewrite ^/profile/([^/]+)/?$ /index.php?a=profile&u=$1;
  rewrite ^/profile/([^/]+)/([^/]+)/?$ /index.php?a=profile&u=$1&r=$2;
  rewrite ^/profile/([^/]+)/filter/([^/]+)/?$ /index.php?a=profile&u=$1&filter=$2;
}

location /notifications {
  rewrite ^/notifications/?$ /index.php?a=notifications;
  rewrite ^/notifications/filter/([^/]+)/?$ /index.php?a=notifications&filter=$1;
}

location /settings {
  rewrite ^/settings/?$ /index.php?a=settings;
  rewrite ^/settings/([^/]+)/?$ /index.php?a=settings&b=$1;
}

location /messages {
  rewrite ^/messages/?$ /index.php?a=messages;
  rewrite ^/messages/([^/]+)/([^/]+)/?$ /index.php?a=messages&u=$1&id=$2;
}

location /track {
  rewrite ^/track/([^/]+)/?$ /index.php?a=track&id=$1;
  rewrite ^/track/([^/]+)/edit/?$ /index.php?a=track&id=$1&type=edit;
  rewrite ^/track/([^/]+)/report/?$ /index.php?a=track&id=$1&type=report;
  rewrite ^/track/([^/]+)/stats/?$ /index.php?a=track&id=$1&type=stats;
  rewrite ^/track/([^/]+)/likes/?$ /index.php?a=track&id=$1&type=likes;
  rewrite ^/track/([^/]+)/stats/filter/([^/]+)/?$ /index.php?a=track&id=$1&type=stats&filter=$2;
  rewrite ^/track/([^/]+)/([^/]+)/?$ /index.php?a=track&id=$1&name=$2;
}

location /playlist {
  rewrite ^/playlist/([^/]+)/?$ /index.php?a=playlist&id=$1;
  rewrite ^/playlist/([^/]+)/edit/?$ /index.php?a=playlist&id=$1&edit=true;
  rewrite ^/playlist/([^/]+)/([^/]+)/?$ /index.php?a=playlist&id=$1&name=$2;
}

location /search {
  rewrite ^/search/filter/([^/]+)/([^/]+)/?$ /index.php?a=search&filter=$1&q=$2;
}

location /page {
  rewrite ^/page/([^/]+)/?$ /index.php?a=page&b=$1;
}

location /recover {
  rewrite ^/recover/?$ /index.php?a=recover;
  rewrite ^/recover/do/?$ /index.php?a=recover&r=1;
}

location /image {
  rewrite ^/image/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /image.php?t=$1&w=$2&h=$3&src=$4;
}

I'm confused and I can't think of anything to figure this out.
Thank you

Best Answer

The rewrite rule convertors are quite bad usually. It is better to implement then in native nginx way. In this case, the image location would be as follows:

location ~ ^/image/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ {
    try_files /image.php?t=$1&w=$2&h=$3&src=$4 =404;
}

BTW. I hope your image.php has proper input validation for width / height, so that attacker cannot DDoS your server by asking 1M x 1M version of an image...

Related Topic