Translate htaccess rewrite rule for nginx - .htaccess

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

Related

.htaccess URL internal Rewrite to (all)-Subdirectory

I'm currently have this:
RewriteEngine On
RewriteBase /
RewriteRule ^(index\.php)?$ /test/ [L]
(index.php seems to be necessary, it's from here:
How can I use .htaccess rewrite to redirect root URL to subdirectory?)
System: MacBook Pro - XAMPP 8.1.2 (installer not VM)
I’m trying to rewrite not redirect (not changing adressbar), everything to /test/
This is working:
Typing in:
localhost/
should show content of
localhost/test/
This is not working:
with all subdirectories localhost/test2/ -> localhost/test/test2/ …
Ok this works (as far as I understand 🙈):
Prevent endless redirecting and getting path (with file (complete
URI))
RewriteCond %{REQUEST_URI} !^/subdirectory/
Rewrite everything to /subdirectory/ + requested path/file
RewriteRule (.*) /subdirectory/%1 [L]
%1 get's URI from RewriteCond.
That's necessary 'cause (.*) won't give us the whole URI (path + index.html ...), it's just the path.
And the same for nginx:
if ($uri !~ "^/subdirectory/"){
rewrite /(.*) /subdirectory$uri last;
}

Apache .htaccess How to convert to nginx htaccess?

apache .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>
Please help me. This problem has bothered me for 3 days. Thank you very much
I try to convert it into the following
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1 last;
}
Authorization cannot be obtained correctly on nginx,Can cause CORS problems
I think it's because this sentence hasn't been converted successfully
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
because I don't know how to convert it
Please help me
I expect to get the converted nginx htaccess
The .htaccess you're seeking to convert is quite standard "front controller" pattern. What it does is, for any URI, it will attempt to:
serve the file matching the exact URI
if the file wasn't found in the previous step, then check a directory, and attempt to serve from there (subject to index directives or further rewrites)
if the directory wasn't found, route the request to index.php
The de-facto standard NGINX snippet that does the same, utilizes try_files directive.
You'll find this used for virtually all PHP-based CMS configs for NGINX:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
The try_files directive, similar to if when being checked for file existence, has its performance pitfalls, so in fact, you may want to do as the article says is route unconditionally all request via PHP-FPM by default, then some (e.g. static assets) served by NGINX directly. Example bare-bones config for WordPress which illustrates the idea:
index index.html index.php;
location / {
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
include fastcgi_params;
# override SCRIPT_NAME which was set in fastcgi_params
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_pass unix:/var/run/php-fpm/example.com.sock;
}
location /wp-content/ { }
In this case /wp-content/ is allocated as an empty location in order to ensure NGINX does not pass requests to files in it via PHP-FPM. It will serve everything directly for static files.
Thus the URL rewriting is actually eliminated, and all the performance penalty that comes with it is negated.
This is the answer
if (!-f $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
if (!-d $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}

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.

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 ?

Resources