How to edit url without updating in browser - nginx config - nginx

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

Related

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

Unable to add/load Nginx subdomain (subdomain repeats)

The long and short of it is, I've added a subdirectory to my code (/main-site/sub) and I wanted to add it to my nginx config. However, I can't stop the subdomain from loading incorrectly (some.web.site/sub/sub/index.html) loads instead of (some.web.site/sub/index.html).
Here is the config file:
server {
listen 80;
server_name site.com www.site.com ~^((?<subdomains>.+)?\.)?site.com$;
root /srv/site;
index index.html index.htm;
access_log /var/log/nginx/site/access.log;
error_log /var/log/nginx/site/error.log;
if ($subdomains !~* "^(www)?$") {
rewrite ^/(.*)$ https://$subdomains.site.com/$1 redirect;
}
rewrite ^/(?!index)(.*).html$ $1 permanent;
rewrite ^/stuff1$ /st1 redirect;
rewrite ^/stuff2$ /st2 redirect;
rewrite ^/stuff3$ /st3 redirect;
rewrite ^/stuff4$ /st4 redirect;
rewrite ^/stuff5$ /st5 redirect;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to 404
# hide html extension
try_files $uri.html $uri/ =404;
}
## Where this stays in or not the result is the same
## site.com/sub/sub/index and not site.com/sub/index
## location /sub/ {
## root /srv/site;
## }
# Redirect 404 to index
error_page 404 = #fallback;
location #fallback {
rewrite .* / permanent;
}
}
Thank you!
Here's the solution:
rewrite ^/(?!index)(.*).html$ /$1 permanent;
I had to add a / before the $1.

Combining two nginx hosts into one

Is it possible to combine these hosts into one?
server {
server_name www.website.com;
rewrite ^ http://website.com$request_uri? permanent;
}
server {
server_name www.website.ru;
rewrite ^ http://website.ru$request_uri? permanent;
}
yes, the following should work:
server {
server_name www.website.com website.com www.website.ru website.ru;
if ( $host ~ "www\.(.*)" ) {
set $hostdomain $1;
rewrite ^ $scheme://$hostdomain$request_uri? permanent;
}
# handling of the non-rewritten non-www requests goes here
}
note: the reason you need to save the domain-minus-www instead of using $1 directly in the rewrite is because the rewrite directive resets the capture variables

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.

nginx : rewrite rule to remove /index.html from the $request_uri

I've seen a few ways to rewrite the $request_uri and add the index.html to it when that particular file exists in the file system, like so:
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
but i was wondering if the opposite is achievable:
i.e. when somebody requests http://example.com/index.html, they're redirected to http://example.com
Because the nginx regexp is perl compatible, i tried something like this:
if ( $request_uri ~* "index\.html$" ) {
set $new_uri $request_uri ~* s/index\.html//
rewrite $1 permanent;
}
but it was mostly a guesswork, is there any good documentation describing the modrewrite for nginx ?
I use the following rewrite in the top level server clause:
rewrite ^(.*)/index\.html$ $1 permanent;
Using this alone works for most URLs, like http://example.com/bar/index.html, but it breaks http://example.com/index.html. To resolve this, I have the following additional rule:
location = /index.html {
rewrite ^ / permanent;
try_files /index.html =404;
}
The =404 part returns a 404 error when the file is not found.
I have no idea why the first rewrite alone isn't sufficient.
The following config allowed me to redirect /index.html to / and /subdir/index.html to /subdir/:
# Strip "index.html" (for canonicalization)
if ( $request_uri ~ "/index.html" ) {
rewrite ^(.*)/ $1/ permanent;
}
For some reason most of the solutions mentioned here did not work. The ones that worked gave me errors with missing / in the url. This solution works for me.
Paste in your location directive.
if ( $request_uri ~ "/index.html" ) {
rewrite ^/(.*)/ /$1 permanent;
}
This one works:
# redirect dumb search engines
location /index.html {
if ($request_uri = /index.html) {
rewrite ^ $scheme://$host? permanent;
}
}
For the root /index.html, the answer from Nicolas resulted in a redirect loop, so I had to search for other answers.
This question was asked on the nginx forums and the answer there worked better.
http://forum.nginx.org/read.php?2,217899,217915
Use either
location = / {
try_files /index.html =404;
}
location = /index.html {
internal;
error_page 404 =301 $scheme://domain.com/;
}
or
location = / {
index index.html;
}
location = /index.html {
internal;
error_page 404 =301 $scheme://domain.com/;
}
This is working for me:
rewrite ^(|/(.*))/index\.html$ /$2 permanent;
It covers both the root instance /index.html and lower instances /bar/index.html
The first part of the regex basically translates as: [nothing] or /[something] - in the first case $2 is an empty string so you redirect to just /, in the second case $2 is [something] so you redirect to /[something]
I actually went a bit fancier to cover index.html, index.htm, and index.php
rewrite ^(|/(.*))/index\.(html?|php)$ /$2 permanent;
The solutions quoting $scheme://domain.com/ assume that the domain is hard-coded. It was not in my case and so I used:
location / {
...
rewrite index.html $scheme://$http_host/ redirect;
... }

Resources