Rewrite configuration issue in a Nginx server - nginx

I'm configuring an Nginx server with both http and https services. I'm trying to achive the following configuration:
redirect every page to HTTPS, except for the home page
In my "http" server configuration, I have already the second rewrite condition working, but I cannot find the way to write the first.
location = / {
what goes here???
}
location / {
rewrite ^(.*) https://mydomain.com$1 permanent;
}
Ideas?

Zenofo's answer should mostly work (just needs the regex "!~*" instead) but will redirect requests that include the name of the home page along with the others.
Using "$uri" in place of "$request_uri" and spelling out the home page file name in the regex gets around this.
location / {
if ($uri !~* ^/index.html)
{
# Redirect non home page requests
rewrite ^ https://$host$request_uri? permanent;
}
# Process homepage requests
....
}
If running php where everything goes through index.php (front end controller) then you can use
location / {
if ($uri !~* ^/index.php$)
{
# Redirect non home page requests
rewrite ^ https://$host$request_uri? permanent;
}
# Process homepage requests
....
}

using $request_uri,like this: (I haven't tested)
location / {
if ($request_uri != ^/$)
{
rewrite ^(.*) https://mydomain.com$1 permanent;
}
}

Related

Nginx rule to redirect to a blog subdomain just spins

I have tried some Nginx rewrite for my new site
Blog
www.qwerty.com/blog/category/educator-blog/
blog.qwerty.com/educator/
DesiredURL, Actual URL, Nginx_Rule
server {
server_name www.example.com/blog/category/educator-blog/;
return 301 $scheme:// blog.example.com/educator/$request_uri;
}
Also tried this :
location /educator {
rewrite ^/educator(.*)$ http://www.qwerty.com/blog/category/educator-blog/$1 redirect;
rewrite ^/educator-categoryid-(.*)-productid-(.*).htm$ /educator.php?categoryid=$1&productid=$2;
}
I've rebuilt with the applied rules but it seems to just spin, some fixed URL. Does the order of the rules matter?

Permanent redirection between folders with nginx?

On the nginx configuration file
default
at
/etc/nginx/sites-enabled/
I am trying to redirect requests made to the folder "home" to another folder "jp".
Following the nginx manual,
I tried the script below. Any ideas as to why this would't work? Thanks.
server{
...
server_name _localhost;
location /home/ {
rewrite www.example.io/home/$ www.example.io/home/jp/ permanent;
}
}
You want a permanent redirection from /home/ to /home/jp/.
The first parameter to a rewrite directive is a regular expression which is matched against a normalized URI, in your case /home/.
You can use a rewrite directive, for example:
location /home/ {
rewrite ^/home/$ /home/jp/ permanent;
...
}
Alternatively, you could use an exact match location with a return statement, for example:
location = /home/ {
return 301 /home/jp/;
}

Nginx : detect mobile and forward to desktop

I have a problem i want to detect the domain and the device and forward the response to the desktop, www server section of nginx. With a rewrite this is done but the problem is the url, is cleared and substitute with my url, the configuration is the following :
server {
listen 80 default_server;
#this detects any domain
server_name ~^(www\.)?(?<domain>.+)$;
root /home/someone/main;
index index.html index.htm;
set $mobile_rewrite do_not_perform;
# this regex string is actually much longer to match more mobile devices
if ($http_user_agent ~* '(iPhone|iPod|iPad|Android|BlackBerry|webOS|Windows Phone)') {
set $mobile_rewrite perform;
}
location / {
if ($mobile_rewrite = perform) {
rewrite ^ http://m.someone.domain.com$request_uri? redirect;
break;
}
}
}
I tried a different approach with an alias to the path in so of the m.someone.domain.com code section but this doesn't work inside the if witch is inside the location.
You can try modifying your if block in following way.
location / {
if ($mobile_rewrite = perform) {
return 301 http://m.someone.domain.com$request_uri;
}
}
As you are using rewrite rule as redirect so above may work. Also rewrites are heavier.
location / {
if ($mobile_rewrite = perform) {
rewrite ^http://m.someone.domain.com$request_uri? / permanent;
}
}
Try this it might work for u

how do I rewrite a URL in nginx to an updated location?

I have the following URLs:
1) http://example.com/downloads/
2) http://example.com/downloads/widgets
3) http://example.com/downloads/gadgets
These URLs need to be redirected rewritten to the following:
1) http://example.com/products/
2 & 3 & etc) http://example.com/products/thingies
I'm currently trying the following nginx code:
location ~* ^/downloads/ {
rewrite ^/downloads/$ /products break;
rewrite ^/downloads/(.*)$ /products/thingies break;
}
It's almost working, however my site's document root is /var/www/example.com/public. So after processing the rewrite rules, nginx tries to literally serve /var/www/example.com/public/products/, whereas I want it to just rewrite to http://example.com/products/ (which then proxies to PHP and so on).
Where am I failing? Is there a different, better way to accomplish this?
Thank you for any help.
-- UPDATE --
I got it to work by using the following rules:
rewrite ^/downloads/?$ $scheme://$host/tools last;
location ~* ^/downloads/ {
rewrite ^/downloads/?$ $scheme://$host/products last;
rewrite ^/downloads/(.*)$ $scheme://$host/products/thingies last;
}
IS this the proper way of doing it in nginx? I haven't seen this rewrite rule format anywhere while researching this. It somehow seems odd.
Your update redirects, not rewrites.
Here is how I would do:
location /downloads/ {
rewrite ^ /products/thingies;
}
location = /downloads/ {
rewrite ^ /products/;
}
# uncomment if you need '/downloads' (without trailing slash) act as '/downloads/'
#location = /downloads {
# rewrite ^ /products/;
#}
The correct syntax appears to be:
location ~* ^/downloads/ {
rewrite ^/downloads/?$ /products permanent;
rewrite ^/downloads/(.*)$ /products/thingies permanent;
}

Nginx Rewrite: change only index location?

How can I send a user to a new location, only if the user have no URI? I am trying the follow, but it does not works... it always send me to /newlocation
rewrite ^/$ http://www.domain.com/newlocation permanent;
rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
So basically, what I need is:
If the user writes on browser www.domain.org it sends to www.domain.com/newlocation
If the user writes on browser www.domain.org/something it sends to www.domain.com/something
Thanks!
I'm not sure why your current approach isn't working. ^/$ should only match /. Maybe it's something else it the current config. Here's a server that should do what you want.
server {
server_name www.domain.org;
# Only match requests for /
location = / {
rewrite ^ http://www.domain.com/newlocation permanent;
}
# Match everything else
location / {
rewrite ^ http://www.domain.com$request_uri? permanent;
}
}

Resources