Convert .htaccess to nginx, seeking working solution - apache

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 :)

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:
gzip_static
try_files

Related

convert nginx to htaccess(apache)

I got the following rewrite rules in Nginx in which im trying to convert for apache/.htaccess rules in which i was keep getting errors and didn't work out well, didnt find any online convertor from nginx to .htaccess only the opposite, Im hoping if anyone could help me out converting it.
This is my nginx rules:
if (!-d $request_filename){
set $rule_0 1$rule_0;
}
if (!-f $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite ^/(.*?)([^/]*)$ /$1index.php?$2 last;
}
this is my .htaccess try:
<IfModule mod_rewrite.c>
RewriteEngine on
# Apache 2.4
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)([^/]*)$ $1index.php?$2 [QSA,PT,L]
RewriteEngine On
RewriteBase /
if (!-d $request_filename){
set $rule_0 1$rule_0;
}
if (!-f $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite ^/(.*?)([^/]*)$ /$1index.php?$2 last;
}
ErrorDocument 404 /404.php
</IfModule>
but i keep on getting parse error whenever i try to do this, anyone have experience to sort this out?
You appear to have already done it, but for some reason you have also embedded the original Nginx directives in the middle (which will naturally result in a parse error).
So, the equivalent Apache/.htaccess directives would seem to be the following only:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)([^/]*)$ $1index.php?$2 [QSA,L]
The PT flag is not required in a .htaccess context.
You do not need the <IfModule> wrapper, unless these directives are optional.
if (!-d $request_filename){
set $rule_0 1$rule_0;
}
However, the first set directive implies that the $rule_0 variable might have already been set earlier in the config?

HTACCESS to NGINX Conversoin

I'm trying to convert the following HTACCESS file into NGINX. I'm on a Plesk server using the built-in converter through the Plesk GUI and it does not seem to be working (keeps generating an error due to the SetEnvIf variable).
The converters I've tried all keep the SetEnvIf variable which does not work in NGINX. NGINX uses an "env" equivalent I believe, however, when I input that into the Plesk GUI for NGINX Settings (which in turn writes to the nginx.conf file in our main server configuration), I get another error message that states we cannot use the "env" variable in this location.
Here is the HTACCESS file we need to convert:
<IfModule mod_setenvif.c>
SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
</IfModule>
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /discuss/api/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
</IfModule>
This is what the converter, and others, create:
if (!-f $request_filename){
set $rule_0 1$rule_0;
}
if (!-d $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
setenv HTTP_AUTHORIZATION:$http_authorization;
rewrite /.* /index.php last;
}
What are we doing wrong (and the converters)?! Thanks in advance.
The context of env is main so it can't be used in virtual server:
env FOO=BAR; // main context
http {
server {
// we a here
}
}
But you can access HTTP_AUTHORIZATION and set environment variable from PHP code in index.php:
<?php
putenv('HTTP_AUTHORIZATION=' . $_SERVER['HTTP_AUTHORIZATION']);

How to convert rewrites from advanced .htaccess to nginx conf rules

Recently switched to nginx from Apache. This works under apache perfectly fine, but don't know how to add it for nginx. Have tried htaccess to nginx converters, but they get me redirect loop.
I have WordPress in root and custom code under subdirectory.
This is the working .htaccess file on Apache:
# rewrite engine on and setting up base
RewriteEngine On
RewriteBase /leffa/
# replace + with _
RewriteRule ^(.+?)\+(.+)$ $1-$2 [R=301,L,NE]
# external redirect from action URL to pretty one
RewriteCond %{THE_REQUEST} /index(?:\.php)?\?q=([^\s&]+)&(kuvauksesta)= [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /index(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ %1? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual URL (extra parameter)
RewriteRule ^([^/.]+)/([^/.]+)?$ index.php?q=$1&$2=1 [L,QSA]
# internal forward from pretty URL to actual URL
RewriteRule ^(.+)/?$ index.php?q=$1 [L,QSA]
This rewrites all the urls like http://www.rollemaa.org/leffa/index.php?q=the+boy to pretty ones like http://www.rollemaa.org/leffa/the-boy.
The situation is, I have main file set up like this:
server {
listen 80;
access_log /var/log/nginx/rollemaa.access.log;
error_log /var/log/nginx/rollemaa.error.log;
root /var/www/rollemaa.org/public_html;
index index.html index.htm index.php;
server_name rollemaa.org www.rollemaa.org;
include hhvm.conf;
include global/wordpress.conf;
# Ensure requests for pagespeed optimized resources go to the pagespeed
# handler and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
# Static File Caching
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
#location /leffa/ {
#try_files $uri $uri/ /leffa/index.php?q=$args;
#rewrite ^/leffa/(.+?)\+(.+)$ /leffa/$1-$2 redirect;
#rewrite ^/leffa/(.*)$ /leffa/%1/%2? redirect;
#rewrite ^/leffa/(.*)$ /leffa/%1? redirect;
#if (-e $request_filename){
# rewrite ^/leffa/([^/.]+)/([^/.]+)?$ /leffa/index.php?q=$1&$2=1 break;
#}
#rewrite ^/leffa/(.+)/?$ /leffa/index.php?q=$1 break;
#}
}
As you can see, I have commented out the rewrite part, because it's not working.
Any nginx gurus out there who could help me with this? Much appreciated in advance!
You can use the following rewrite rule.
location /leffa/ {
index index.php index.html;
rewrite ^/leffa/([^/]*)/?$ /leffa/index.php?q=$1;
}
It will rewrite URLs like /leffa/the-boy/ and /leffa/the-boy to /leffa/index.php?q=the-boy. URLs with sub-subdirectories such as /leffa/the-boy/something-here will be ignored.
Note: + in the URL is not converted to a space (as it would be when directly accessing /leffa/index.php?q=the+boy). Accessing /leffa/the+boy/ will result in the query parameter q being "the+boy".
If you want to use spaces in the query string, you will have to use the %20 URL encoded format for spaces. /leffa/the%20boy and /leffa/the%20boy/ will result in the query parameter q being "the boy".

converting apache htaccess to nginx

I am trying to convert apache htaccess rule to nginx config but it is not working. Following are the details of rules and nginx config:
.htaccess Rule
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Nginx Config For Above
server {
listen ip_address:80;
server_name www.example.com;
access_log /var/log/nginx/example.com-access.log;
error_log /var/log/nginx/example.com-error.log;
root /var/www/html/example.com/;
index index.php;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php/$1 break;
}
}
when i access website it opens home page and when i click other menus it gives 404 not found.
Following is the error log:
2015/03/18 05:31:56 [error] 3550#0: *7 open() "/var/www/html/example/index.php//contents.html" failed (20: Not a directory), client:
1.2.3.4, server: www.example.com, request: "GET /contents.html HTTP/1.1", host: "www.example.com", referrer: "http://example.com/"
any ideas???
Shoaib..
Your nginx rewrite is wrong – because of the slash in the end, nginx thinks index.php is a folder. You could do something like:
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?url=$1 break;
}
}
And then in your PHP code parse $_REQUEST['url']
Another possibility, many webmasters are doing it this way:
location / {
try_files $uri $uri/ /index.php?$args;
}
Meaning – try to open a file first (if it exists), try to open a folder (if it exists), last resort – pass it to index.php. $args will contain query string, you can access original URL through $_SERVER.
Issue has been resolved, we enabled following in website's config.php file,
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
And nginx config as follows,
location / {
try_files $uri $uri/ /index.php?$args;
}
it worked...
Thanks #Denis and #SuddenHead for your replies.

Converting apache rewrite rules (.htaccess) to nginx

Background:
I want to setup Bugify on my Ubuntu Server running nginx. I followed the instructions, installed the dependencies and the installation was successful. Once I enter my licence key and click on "Install Bugify" it's redirecting to http://dev.mysite.com/login?new and the only thing I'm seeing is 404 Not Found.
I know that nginx isn't officially supported but according to the support forum it should be possible to run it.
Question:
There's a .htaccess file with rewrite rules in the webapp's public directory and I guess the problem causing the 404 error is that nginx isn't able to work with the .htaccess file, so I'll have to convert the rewrite rules to the nginx syntax.
I'm not really familiar with apache's rewrite rules so I'd need some help figuring this out.
The content of the .htaccess file is:
# Rewriting
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I was using this tool to convert the rules but it's having some troubles with the - flags.
#ignored: "-" thing used or unknown variable in regex/rew
Thanks in advance!
Bugify uses the Zend Framework, so the rewrite rules for ZF should work for Bugify also. I have copied the suggested nginx config below from http://wiki.nginx.org/Zend_Framework
server {
listen 80;
server_name www.example.com;
root /var/www/www.example.com/myapplication;
index index.html index.htm index.php;
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/usr/local/zend/tmp/php-fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/www.example.com/myapplication$fastcgi_script_name;
include fastcgi_params;
}
}