exclusion of index.html in nginx rewrite rule - nginx

I have a website which I am rendering in https mode and I have written the rewrite rule in the default.conf like below:
listen 80;
server_name hostname.com;
rewrite ^ https://$server_name$request_uri? permanent;
However, I need to exclude the index.html or / from rewriting. How can I write the rule to exclude the index.html or / from rewriting?

I see no reason for this, but here is example which excludes / and /index.html from rewrite. Also I've changed rewrite to more efficient return directive.
listen 80;
server_name example.com;
root /path/to/root/dir;
# exclude / from rewrite
location = / {
index index.html;
}
# exclude /index.html from rewrite
location = /index.html {
}
location / {
return 301 https://$host$request_uri;
}

Related

nginx subdomain rewrite to a given url (with params)

i'm trying to setup a custom nginx rewrite rule,
in order to match a given subdomain with it's partner id route.
URL: https://subdomain.doma.com >
should display https://subdomain.domain.com/?partner=subdomain
Here is my config:
##################
server {
listen 80;
server_name ~^(?<subdomain>.+?)\.domain\.com$;
root /var/www/domain.com;
index index.html;
location / {
try_files $uri $uri/ /index.html;
rewrite ^ /?partner=$subdomain break;
}
##################
Thank you in advance.

Rewrite directory as parameter

I'm trying to redirect requests from
example.com/abc234 to
example.com/setup.html?s=abc234
So far, I've tried the following, but it seems to always end up either 1) not transmitting the parameter or 2) ending up in an infinite loop (or 404) because it also tries to redirect the redirected request? The request has to be visibly rewritten because I want to pick up the parameter with JS, not PHP.
server {
server_name example.com;
root /var/www/html;
rewrite ^(.*)$ /setup.html?s=$1 redirect;
}
I've also tried various combinations of location / { try_files ...; } or using the absolute URL within rewrite without success.
One technique is to rewrite only URIs that do not match a physical file.
For example:
server {
server_name example.com;
root /var/www/html;
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
return 302 /setup.html?s=$uri;
}
}
See this document for more.
rewrite ^(.*)$ /setup.html?s=$1 redirect;
}

Simple url rewriting

I have a simple index.html page with some javascript.
On my nginx config, I would like to apply this rule :
Rewrite http://www.mywebsite.com/?l=A1B2C3 to http://www.mywebsite.com/A1B2C3
Here is my file (I got a 500 error) :
server {
listen 80;
root ...;
index index.html;
server_name www.mywebsite.com;
location / {
rewrite ^(.*)$ /?l=$1 last;
break;
}
}
Try this:
location / {
rewrite ^/([A-Za-z0-9_]+)$ /?l=$1 last;
}

Nginx 301 Redirect all non home page urls to home page

I'm shutting down a site and I need to 301 redirect all pages to the home page where I have a message saying that the site is being closed down.
Basically any http://example.com/anyfolder -> 301 to http://www.example.com
I have the following but this results in a redirection loop.
location ~ ^/([A-z]+) { rewrite ^ / permanent; }
What is the proper way to do this in nginx?
Make it simple.
location / {
rewrite ^ http://example.com;
}
location = / {
}
This worked for me. This is assuming you only have a index.html,htm and the other urls are missing the physical file on disk.
server {
listen 80;
server_name www.example.com example.com;
root /u/apps/example/www;
index index.html;
location / {
if (!-e $request_filename) {
rewrite ^ / permanent;
}
}
}
The below is a more modern syntax for trying various file resources for existence, with progressive defaults.
Be sure to uncomment other location paths underneath the one below, to avoid more specific matches, if necessary.
Also, if is evil when used in location context.
server {
listen 1.2.3.4:80;
server_name example.com;
root /path/to/document/root;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}

How to set up Nginx config to rewrite my subdomain

I'm try to set up the subdomains with Nginx, but I get some error. The following is my config:
server {
listen 80;
server_name dimain.com *.domain.com;
root /path/to/fuelphp_project/public;
location / {
index index.php index.html index.htm;
if ($host ~ (.*)\.(.*) ) {
set $sub_name $1;
rewrite ^/(.*)$ /index.php/$sub_name/$1 last;
break;
}
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
break;
}
}
...
}
I want to the results like:
sub1.domain.com/c1/a1 -> domain.com/sub1/c1/a1
sub2.domain.com/c2/a2 -> domain.com/sub2/c2/a2
How to correct it?
server {
server_name sub1.domain.com;
return 301 "http://domain.com/sub1${uri}";
}
Would this work for you?
I just noticed an answer to this here: https://serverfault.com/questions/426673/nginx-redirect-subdomain-to-sub-directory
server {
server_name ~^(?<sub>[a-zA-Z0-9-]+)\.domain\.com$; # will cause the sub-domain to be placed in $sub
return 301 "http://domain.com/${sub}${uri}";
}
I think the regular expression also can be a very cool solution... Depends what you need.

Resources