Converting .htaccess rewrites to NGINX configuration - .htaccess

I am having difficulties converting two RewriteCond 'blocks' of rewriterules to NGINX configuration, and would like a fresh view on this. The blocks of rewriterules I am trying to convert to NGINX look like this:
# Production
RewriteCond %{HTTP_HOST} ^www\.domain\.nl$ [NC]
RewriteRule ^(_next/.*)$ https://lp.domain.nl/$1 [P]
RewriteRule ^(assets/next/.*)$ https://lp.domain.nl/$1 [P]
RewriteRule ^(lp2/.*)$ https://lp.domain.nl/$1 [P]
RewriteRule ^(advice/welcome)$ https://lp.domain.nl/$1 [P]
RewriteRule ^(RewriteRule ^(api/.*)$ https://lp.domain.nl/$1 [P]
RewriteRule ^api/v1/(.*)$ https://app.domain.nl/api/v1/$1 [P]

I would recommend capturing the https redirect paths using a map. Using regular expressions in a map minimizes their impact on the flow of your config.
map $uri $redirect_https {
~^/(?<path>_next/.*) lp.domain.nl/$path;
~^/(?<path>assets/next/.*) lp.domain.nl/$path;
~^/(?<path>lp2/.*) lp.domain.nl/$path;
~^/(?<path>api/v1/.*)$ app.domain.nl/$path;
# default api check should come last
~^/(?<path>api/.*) lp.domain.nl/$path;
}
server {
server_name www.domain.nl;
listen 80;
location = /advice/welcome {
return 301 https://lp.domain.nl/advice/welcome;
}
if ($redirect_https) {
return 301 https://$redirect_https;
}
# ... other non-https config here
}

Related

converting apache rewrite to nginx url rewrite

can anybody help me to convert .htacess file to nginx rewrite rule
my htaccess blew
RewriteEngine On
RewriteRule ^video/([^/]*)/([^/]*)\/$ /index.php?page=details&title=$1&id=$2 [L]
RewriteRule ^video/([^/]*)\.html$ /index.php?page=result&q=$1 [L]
i try apply on here:
/etc/nginx/sites-available/default
# nginx configuration
location /video {
rewrite ^/video/([^/]*)/([^/]*)\/$ /index.php?page=details&title=$1&id=$2 break;
rewrite ^/video/([^/]*)\.html$ /index.php?page=result&q=$1 break;
}
not working..

Conversion from htaccess to rewrite issue

I have moved site from apache to nginx and I have got some issue with nginx.
htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$
RewriteRule ^(.+)/(.+[^/])/(.*[^/])/?$ index.php?p=$1&subp=$2&subsubp=$3 [QSA,L]
RewriteCond %{REQUEST_URI} !\.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$
RewriteRule ^(.+)/(.+[^/])/?$ index.php?p=$1&subp=$2 [QSA,L]
RewriteCond %{REQUEST_URI} !\.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$
RewriteRule ^(.*[^/])/?$ index.php?p=$1 [QSA,L]
Options -Indexes
And using converter to nginx:
location ~ \.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$ {
}
location ~ \.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$ {
}
location ~ \.(js|css|png|jpg|jpeg|bmp|ttf|php|php5|html|htm|gif)$ {
}
autoindex off;
location / {
rewrite ^/(.+)/(.+[^/])/(.*[^/])/?$ /index.php?p=$1&subp=$2&subsubp=$3 break;
rewrite ^/(.+)/(.+[^/])/?$ /index.php?p=$1&subp=$2 break;
rewrite ^/(.*[^/])/?$ /index.php?p=$1 break;
}
I think that, can work when we are not talking about subdomain.
Yes - my site is in subdomain called /mysub/
So to enter my website I use mysub.domain.com or domain.com/mysub/
(where the first is this:)
if ($http_host = "mysub.domain.com") {
rewrite ^(?!/\b(mysub|stats|error)\b)/(.*)$ /mysub/$2 ;
}
Question:
How to link these rules into one ?

rewrite htaccess subdomain

Mysite's running normally with the code rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ https://www.mysite.com/$1 [L,R=301]
#rewrite www.mysite.com/page.php?page=ID => www.mysite.com/ID
RewriteRule ^([a-zA-Z0-9_-]+)/?$ page.php?page=$1
It's running okay, but if the ID has a dot (.), it's not working? So
how do I fix this?
#rewrite www.mysite.com/game/play.php?g=IDgame => www.mysite.com/game/IDgame
RewriteRule ^game/([a-zA-Z0-9_-]+)$ /game/play.php?g=$1
It's working fine :)
I add subdomain for mysite is: m.mysite.com
I want to rewrite:
m.mysite.com/game/play.php?g=IDgame => m.mysite.com/game/IDgame
How do I code this?
Instead of having a list of characters the id might contain, it is often easier to have a list of characters the id certainly cannot contain. In your case it can most likely not contain a slash. You could therefore match with ([^/]+). You want to explicitely exclude play.php from matching as well, which you can do with a rewrite condition.
RewriteCond %{REQUEST_URI} !/game/play\.php
RewriteRule ^game/([^/]+)/?$ /game/play.php?g=$1
As for your m.mysite subdomain. Make sure that the virtual host for both www.mysite and m.mysite point to the same directory. If your dns is configured properly it should internally rewrite your mobile game urls to the same file.

Rewrite url to pretty after redirect to folder - htaccess laravel

Hello I have a "protected" folder on my server. In its .htaccess file for conditional redirect of some users I use the following rules:
RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} !^/special
RewriteRule ^$ /special [R,NE,NC]
In the /special folder I have a .htaccess file with the following rules:
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4$
RewriteRule ^(.*)$ / [R=301,NE,NC,L]
The application in the folder will be laravel based so my content will have to be served from index.php file residing in /special/laravel/public/index.php
I want the URL to look like /special/.
What rules should I put and where for this to happen?
This is a follow up to my previous question:
https://stackoverflow.com/questions/24487012/redirecting-specific-ip-to-special-content-htaccess-vs-php
Simply rewrite the URL with .htaccess: (goes in /)
DirectoryIndex index.php
#Redirect to /special/laravel/public if you haven't already and the IP is okay
RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} !^/special/laravel/public
RewriteRule ^(/special)(/laravel)?(.+) /special/laravel/public$2 [L]
#if IP does not match and you ARE in the folder, then Redirect to root
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} ^/special/laravel/public
RewriteRule .? / [R=301,L]
I think that'll work. I didn't test it though. I can add to it if you need me to. And of course for your use case you may need to add some more RewriteConds in there for validating the REMOTE_ADDR.
The way I handle it:
.htaccess:
RewriteCond %{REQUEST_URI} !^/load_page.php$
RewriteRule ^(.+)$ /load_page.php [QSA, L]
That causes the server to redirect internally to load_page.php if the requested URL is not load_page. Without the RewriteCond, I believe it would cause an infinite redirect. This should work, but I didn't test it because it's different from my implementation, since mine also handles rewriting URLS to have trailing slashes and never show .php which makes it a bit more complex and quite different.
load_page.php:
$SITE_ROOT = $_SERVER['DOCUMENT_ROOT'];
$URL = $_SERVER['REQUEST_URI'];
ob_start();
if (conditionOne){
//change $URL here if you need to or do nothing
} else if (conditionTwo){ //check the IP or whatever you want to here
if (aCondition){//if $URL starts with '/special'
$URL = str_ireplace('/special/','/special/laravel/public/',$URL,1);
}
if (is_dir($SITE_ROOT.$URL))$URL .='index.php';
}
if (!file_exists($SITE_ROOT.$URL))$URL = '/error/404.php';
include($SITE_ROOT.$URL);
$content = ob_get_clean();
echo $content;
It's something to that affect. The user doesn't see load_page.php and they don't see that you changed the URL to special/laravel/public, but you include the correct file.

Translate htaccess rewrite rule for nginx

I would like to translate the following apache htaccess rewrite rule for nginx.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.+) /index.php
Any ideas how to do it?
The apache rewrite rules are explained clearly in the other question, especially that special dash "-" rule for non-substitution. This can be rewritten as a try_files directive in nginx.
Your question doesn't list a rule for the empty string (home page) (only has non-empty string (.+)). So I'll assume you've had one rule for the home page somewhere else. The rule in nginx could be
location = / {
# your nginx rule matching the empty string (for your home page)
}
# all other locations try in the order: file => directory => index.php
location / {
try_files $uri $uri/ /index.php;
}

Resources