Simple url rewriting - nginx

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

Related

How to edit url without updating in browser - nginx config

I want the Request URL to be converted from
http://host.com/newname/abc?def= to
http://newname/abc?def=
here is the config file
server {
listen 80;
server_name
host.com
location / {
rewrite ^(.+)/$ $1 permanent;
rewrite ^(.+)/index\.html$ $1 permanent;
rewrite ^(.+)\.html$ $1 permanent;
try_files /$host/public/$uri #webserver;
}
}
Adding above line worked form me
location / {
rewrite ^ $scheme://$request_uri? permanent;
}
But it replaces the url in user browser which i donn't want to happen.
Any way to achieve it
You can try this:
location /newname{
proxy_pass http://example.com/newname/abc?def=;
}
or
location /newname{
proxy_pass http://newname/abc?def=;
}

Nginx location domain.ltd regex

i need rewrite for domain whois for example http://domainname.ltd/example.com must be call http://domainname.ltd/whois.php?d=example.com
i try location examples but not working.
you can do it like this:
server {
listen 80;
server_name test.com;
location = /whois.php {
proxy_pass http://your_origin_host.com
}
location ~ \/(.*) {
set $domain_query $1;
set $args "d=${domain_query}";
rewrite ^ /whois.php last;
}
}

exclusion of index.html in nginx rewrite rule

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

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