Convert .htaccess to nginx, seeking working solution

99 views Asked by At

Hello i am aware that there are some .htaccess2nginx converter outside, and well, still need some help, since it's not working at all.

To my problem: Setting up an Imageboard(MyImouto) on Nginx, Imageboard uses .htaccess to redirect from /srv/domain/public/ to /srv/domain/app/...

Here is the .htaccess:

RewriteEngine On

# Images redirection.
# The following RewriteCond will try to deny any direct linking to images.
# RewriteCond %{HTTP:Referer} ^http://domain/
RewriteRule ^(?:data/)?(preview|sample|jpeg|image)/([0-9a-f]{2})([0-9a-f]{2})([$

# Serve gzipped Stylesheets and Javascripts if mod_headers are enabled.
<IfModule mod_headers.c>
    SetEnv no-gzip
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(assets/.*)\.css $1\.css\.gz [L,ENV=ASSETSCSS:true] [L]
    Header set Content-Type text/css env=ASSETSCSS
    Header set Content-Encoding gzip env=ASSETSCSS
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(assets/.*)\.js $1\.js\.gz [L,ENV=ASSETSJS:true] [L]
    Header set Content-Type text/javascript env=ASSETSJS
    Header set Content-Encoding gzip env=ASSETSJS
</IfModule>

# Redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]

Converting with one of the 3 converters i used didn't even worked. The other both throw out something like this:

        rewrite ^/(?:data/)?(preview|sample|jpeg|image)/([0-9a-f]{2})([0-9a-f]{2})([$ /;
if ( ~ ""){
    set $rule_1 1$rule_1;
}
if ($rule_1 = "1"){
    rewrite / /;
}
if ( ~ ""){
    set $rule_2 1$rule_2;
}
if ($rule_2 = "1"){
    rewrite / /;
}
if (!-f $request_filename){
    set $rule_3 1$rule_3;
}
if ($rule_3 = "1"){
    rewrite /.* /index.php last;
}

But it's still not working. Would be nice if you could help me for an solution :)

1

There are 1 answers

1
Alexey Ten On BEST ANSWER

First of all, you have to understand what this .htaccess do.

Except of incomplete rewrite this .htaccess tries to serve precompressed assets (file.css.gz instead of file.css) and rewrites requests for non-existent files to index.php.

I would write something like this:

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

location ^~ /assets/ {
    gzip_static on;
}

location ~ \.php$ {
    try_files $uri /index.php;
    # other PHP fastcgi stuff
}

Reference: