Rewrite with multiple back reference with .html file to domain name - nginx

About:
I'm trying to rewrite my domain using Nginx:
I have a website on which I'm trying to rewrite the URL to www.my.domain.com to make it secure as it's showing the whole directory structure
Right now when I write in the browser
www.my.domain.com
It redirects me to this path below
www.my.domain.com/dreamfactory/dist/index.html#/login
I want to hide the directory structure from URL to keep it safe, on search www.my.domain.com
Results should be:
www.my.domain.com/
Implemented Rewrites:
location / {
try_files $uri $uri/ /index.php?$is_args$args;
}
location / {
try_files $uri #rewrite;
}
location #rewrite {
rewrite ^(.*[^/])$ $1/ permanent;
rewrite ^(.*)/$ $1.html break;
}
Any Hope 🤞🤞🤞🤞🤞🤞🤞🤞

Related

Nginx - remove trailing slash if it's not a directory

I have no problems with redirect, but when I uncomment 2 lines that do "if folder doesn't exist" check, I get 404 error on any page. And I don't understand why, it's basic functionality.
I need to redirect all requests with trailing slash to urls without it. The only exception is when a folder with that url exists.
location / {
#if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
#}
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$request_uri;
}
You should read this caution on the use of if.
You could try an alternative approach and only rewrite after try_files has processed the URIs that it can:
location {
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/(.*)/$ /$1 permanent;
rewrite ^ /index.php?q=$request_uri last;
}
See this document for more.

How to use my index.htm in this location script?

I need URL http://myexample.org (root) redirecting to my local index.htm, not rewriting it to Github... How to do it?
I was testing many variations of location = / { try_files ...} but no one works. Using a UBUNTU 16 LTS server.
/etc/nginx/sites-available/myexample.org
server {
server_name myexample.org;
root /var/www/myexample.org;
index index.html index.htm;
# ?? location =/ {...} is not working!
location / {
# also not work a root rewrite
# rewrite ^/?$ index.htm break;
rewrite ^/?git$
http://github.com/myexample-org/test
break;
rewrite ^/?tickets$
http://github.com/myexample-org/test/issues
break;
rewrite ^/?(.+)$
http://github.com/myexample-org/test/$1
break;
}
}
Change your last rewrite directive to match ^/(.+)$
location = / will not work for an index, as the rewritten URI will match location / which will then hit your final rewrite statement.
Your original solution (a few questions ago) with the named location, should work fine:
root /path/to/file;
index index.htm;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/... http://example.com/...;
rewrite ^/... http://example.com/...;
rewrite ^/... http://example.com/...;
}
Assuming that a file called /path/to/file/index.htm exists on this server. The break flag is unnecessary as the destination URL begins with http://. If you want to add a flag, the redirect or permanent flag would be pertinent. See this document for details.

nginx rewrite & parameter full-url and file extensions

I will try to be brief. I have the following nginx rewrite url:
rewrite ^/(.*)?$ /index.php?completeURL=$1 last;
I want a url like:
http://mywebsite.com/http://www.otherwebsite.com/dir1/dirx/article.php&id=2&category=1
request:
http://mywebsite.com/index.php?completeURL=http://www.otherwebsite.com/dir1/dirx/article.php&id=2&category=1
Currently the nginx rule have a problem. Example: If the parameter contains a .php extension he looks for that file on my server.
Example: http://mywebsite.com/dir1/dirx/article.php
How can I solve this problem in your opinion?
UPDATE:
here the nginx configuration (and rewrite) files:
(config) https://gist.github.com/ivanionut/cc53c9de372b932c3937d9394d3b448c
(rewrite) https://gist.github.com/ivanionut/4df3ad9b858a54ae01461ab078adffb6
The simplest solution (assuming that the server does nothing else other than serve index.php) is to remove the usual location ~ \.php$ block and perform a rewrite ... break; in the same block as the fastcgi_pass. There are a number of ways of achieving this, including:
location / {
rewrite ^/(.*)?$ /index.php?completeURL=$1 break;
fastcgi_pass ...
...
}
An alternative strategy is to perform the rewrite only if a local file does not already exist, but you need to ensure that .php files are tested too. For example:
location / {
try_files $uri $uri/ #rewrite;
}
location ~ \.php$ {
try_files $uri #rewrite;
fastcgi_pass ...
...
}
location #rewrite {
rewrite ^/(.*)?$ /index.php?completeURL=$1 last;
}

nginx rewrite URL only if the original one gives 404 error

I've this rewrite rule:
rewrite ^/components/com_jshopping/files/img_products/full_(.*)$ http://www.domain.tdl/components/com_jshopping/files/img_products/$1 permanent;
It redirects every request of images which starts with full_ to the same image without the prefix.
Now I'd like to apply this rule only if the requested image does not exists (404 error).
How can I do?
You could use try_files with named location that will do redirect:
location /components/com_jshopping/files/img_products/full_ {
try_files $uri #redirect;
}
location #redirect {
rewrite ^(/components/com_jshopping/files/img_products/)full_(.*)$ http://www.domain.tdl$1$2 permanent;
}

Nginx rewrites static files to index.html

I'm trying to work on a single page app - I need to rewrite all urls to index.html but allow existing static files (.css and .js) to be served as they normally would be in a browser.
This is the code that I'm trying to use to re-write but it serves my static files to the re-write as well
if (!-e $request_filename)
{
rewrite ^/(.*)$ /?/$1 last;
break;
}
you don't actually need a rewrite for that in nginx, just use try_files like so:
location / {
try_files $uri /index.html;
}
what this does is for all url's:
try the exact static filename match, and serve it if present
if 1 didn't serve anything, then server /index.html instead
see http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
This should work:
server {
listen 1.2.3.4:80;
server_name domain.eu;
root /usr/local/www/domain.eu/public;
try_files $uri #rewrites;
location #rewrites {
rewrite ^/favicon.ico$ /pictures/favicon.ico last;
rewrite ^ /index.html last;
}
}

Resources