Conversion from htaccess to rewrite issue - .htaccess

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 ?

Related

Converting .htaccess rewrites to NGINX configuration

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
}

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

Translating rewriting from htaccess to nginx

I have .htaccess:
RewriteRule ^thumb/(.*)x(.*)/r/(.*) thumb.php?w=$1&h=$2&src=$3
RewriteRule ^medias/(.*) files.php?file=$1
RewriteRule ^sitemap\.xml$ index.php [L]
RewriteRule ^(.*)(\.html|\.htm)$ index.php [L]
RewriteRule ^(.*)(\.rss|\.atom|\.txt)$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]
Nginx rewriting is:
rewrite ^/thumb/(.*)x(.*)/r/(.*) /thumb.php?w=$1&h=$2&src=$3;
rewrite ^/medias/(.*) /files.php?file=$1;
rewrite ^/sitemap\.xml$ /index.php break;
rewrite ^/(.*)(\.html|\.htm)$ /index.php break;
rewrite ^/(.*)(\.rss|\.atom|\.txt)$ /index.php break;
When accessing categories, I get the home page.
Any help please
Thanks
Nginx configuration is typically approached a bit differently, with lots of location blocks.
It's difficult for me to test this, but this might help you forward:
# Nginx excludes the query string (part after the ?) before trying to match
location = /index.php {
# FastCGI PHP stuff here
}
location ~ ^/thumb/([^/]+)x([^/]+)/r/([^/]+) {
rewrite ^ /thumb.php?w=$1&h=$2&src=$3 break;
}
location ~ ^/medias/(.*) {
rewrite ^ /files.php?file=$1 break;
# To be honest I would try to avoid serving files through PHP. It's much more efficient to let Nginx serve them directly with something like:
root /path/to/media's/parent;
try_files $uri =404;
}
location ~ ^/sitemap\.xml$ {
rewrite ^ index.php break;
}
location ~ (\.html|\.htm|\.rss|\.atom|\.txt)$ {
rewrite ^ index.php break;
}
# Catch-all
location / {
try_files $uri index.php;
}
The fact that you have to test for extensions, with regexes, means they kind of all have to use regexes. That's because those blocks with regexes are evaluated before longest-prefix blocks. Make sure to read Nginx' documentation about location blocks for a better explanation, with examples.

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;
}

.htaccess problem

I'm new to apache's mod_rewrite and i'm having a problem ...
If i use the url : mysite.com/nl/ or mysite.com/fr/ i'm getting redirected to the right page, when a user enters the url mysite.com i redirect my user to the nl language but the problem is in the adress bar the url stays mysite.com and i would like it to be mysite.com/nl
this is my htaccess file:
RewriteEngine on
DirectoryIndex index.php
RewriteBase /
# Rewrite voor taal en pagina
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ $1/$2 [R]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?tl=$1&pg=$2 [L]
#rewrite voor taal
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ $1 [R]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?tl=$1 [L]
my folder structure is:
[nl]
index.php
contact.php
[fr]
index.php
contact.php
index.php
the content of index.php (in the root directory):
<?
switch ($_REQUEST['tl'])
{
case 'nl':
switch ($_REQUEST['pg'])
{
case 'contact':
include_once("nl/contact.php");
break;
default:
include_once("nl/index.php");
break;
}
break;
case 'fr':
include_once("fr/index.php");
break;
default:
include_once("nl/index.php");
break;
}
?>
So when a users enters mysite.com it shows the index.php from the nl directory but the url in the browser address stays mysite.com but should be mysite.com/nl/
Since i have zero experience in this rewriting rules i've been stuck at it for hours ...
Thanks in advance,
Davidi
Instead of using include_once try using header('Location: /nl/page.php') or header('Location: /fr/page.php');

Resources