convert nginx to htaccess(apache) - 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?

Related

htaccess to nginx rewrite issue

In my code I am trying to convert Apache .htaccess into Nginx using online convertor. I even edited this file etc/nginx/conf.d/domainname.conf but the rewrite URLS won't work. I have two htaccess files, one of which used in root folder and the second one used in administration folder of my script.
Here is the root's htaccess file content
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?slug=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ index.php?slug=$1
htaccess converted code for Nginx
location / {
rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
}
Here is the administration folder htaccess file content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* $0.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^(.*)/$ /$1.php [L,R=301]
Administration folder htaccess converted code for Nginx
location /administration {
if (!-e $request_filename){
rewrite ^(.*)$ /$0.php break;
}
rewrite ^/(.*)/$ /$1.php redirect;
}
I pasted the converted content into domainname.conf (domainname is my domain) and restart the ngnix but it won't work at all. I don't know whether my converted code is accurate or not or any thing else I am missing through it.
Simply edit the file i.e. etc/nginx/conf.d/yourdomain.tld.conf and look for the following code like this:
#location / {
#Uncomment 3 lines and set your rules here!
#}
You need to un-comment these 3 lines of code or paste this code instead:
location / {
if (!-e $request_filename) {
rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
}
}
location /administration/ {
if (!-e $request_filename){
rewrite ^(.*)$ $1.php last;
}
}
Then you must require to restart Nginx, if restarted without any errors it will work accurately otherwise Nginx will stop working, this step is important.

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".

Htaccess rule to nginx

i'm trying to rewrite the htaccess rule to nginx rule.
this rule is used to load a php page for robot (like google robot).
here my htaccess:
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteCond %{REQUEST_URI} ^/faq/$
RewriteCond %{QUERY_STRING} ^(.*)_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ /files/snapshot_loader.php?snapshot_page=%1%2 [L]
can you help me ? i try this one, but redirect never work....
location /faq {
if ($query_string ~ "^(.*)test(.*)$"){
RewriteRule ^(.*)$ http://www.google.com permanent
}
}
Thanks in advance
First, there is no $query_string variable in nginx. And the second thing is there is no RewriteRule directive in nginx.
In nginx, the query string is stored in $args and you want the rewrite directive:
Try something like:
location /faq {
if ($http_host ~* "^localhost$") {
if($args ~* "^(.*)_escaped_fragment_=(.*)$") {
set $newargs $1$2
set $args '';
rewrite ^ /files/snapshot_loader.php?snapshot_page=$newargs
}
}
}

Convert htaccess rules routing paths to index.php to nginx

I'm new to nginx, but I'm trying to convert
# Disallows file browsing
Options -Indexes
# Custom Error Pages
ErrorDocument 403 /error-docs/403.php
ErrorDocument 404 /error-docs/404.php
ErrorDocument 500 /error-docs/500.php
# Turn on the Apache Rewrite Engine
RewriteEngine On
#Conditional to see if it is a real file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If not then pass the query string to index.php and stop any further rules
RewriteRule (.*) index.php/$1 [PT,L]
to nginx syntax. I've tried the suggestion at Routing requests through index.php with nginx but it doesn't do quite what I need it to do. I've also tried the auto converters http://winginx.com/en/htaccess and http://www.anilcetin.com/ but the rules they output don't work for me either. Any help is appreciated.
The issue I had was actually related to my PHP and MySQL code.
location / {
try_files $uri $uri/ /index.php$args;
}
works fine. Thanks for the link.