nginx rewrite: add index.php - nginx

how do I do a rewrite in nginx from
http://www.example.com (with or without trailing slash)
to add index.php at the end?
For the example above the result should be
http://www.example.com/index.php
No special rules, just one domain to change.This should be simple but I can't seem to figure it out :-(
Thanks!

Assuming you mean you want the user's address bar to change, you just need one extra location in your server block:
location = / {
return 301 http://www.example.com/index.php;
}

Related

NGINX -Rewrite url keeping part of url

Please help me to create nginx redirect like below:
https://mypage.com?via=181696076 -> https://newpage.com/checkout/?cc=GS950N&short_id=gi&via=181696076
The parameter(181696076) will change, will be only a number and will not have a fixed length
Can I do this using location?
rewrite ^/(.*)$ https://newpage.com/checkout/?cc=GS950N&short_id=gi&$1;
I tried few combination, but it does not work for me - like the rewriting on server level
Thanks
I found such solution for this problem:
location ~ ^/(.*)$ {
return 301 https://newpage.com/checkout/?cc=GK960N&short_id=gi&via=$1;
}

Chage the part of the URL using nginx

I m using nginx webserver.
I want to change the url before it hits the server from
https://www.example.com/abc/contact-us
to
https://www.example.com/#/contact-us
Thanks in advance.
For a single URI redirection, an exact match location and return statement may be most efficient:
location = /abc/contact-us {
return 301 /#/contact-us;
}
To redirect all URIs beginning with /abc use a rewrite directive:
location ^~ /abc/ {
rewrite ^/abc(.*)$ /#$1 permanent;
}
The location block is largely redundant, but means nginx only looks at the regular expression when it needs to. See this document for more.

Nginx Redirect for WooCommerce Checkout Endpoint

I would like to ask how it would be the rewrite rule for nginx for adding to a variable directory at the end of an address a slash. Specificaly i want to solve a 404 error that occurs when i use WooCommerce with nginx - php-fpm.
The slash to be added is at the checkout endpoint and it would look like this
http://xxxx.xx/checkout/xxx?key=wc_order_5702b88b72e77
to rewrite to
http://xxxx.xx/checkout/xxx/?key=wc_order_5702b88b72e77
where the "xxx" after the /checkout/ is the order number and it is a variable.
Thanks in advance.
I think you would want something like this:
server {
# put this before your locations
rewrite ^(/checkout/[a-z0-9]+)$ $1/ permanent;
}
I assume that the order id contains numbers and lower case characters only. You might want to change this by changing the regex. 0-9 match numbers, A-Z uppercase characters and a-z lowercase characters.
You can also put the rewrite in a location block to make it more specific.
location ~ ^/checkout/[a-z0-9]+$ {
rewrite ^(/checkout/[a-z0-9]+)$ $1/ permanent;
}
Additionally, instead of using the permanent flag that permanently redirects requests (301 redirect) you can use the flag last which stops processing the current set and the rewritten request is passed once again to find the appropriate location.

Rewrite old image url with nginx

I have a single image that I renamed and need to redirect from the old image url to the new one my server uses nginx, I'm not having much success with the following rewrite:
rewrite ^/assets/avatar/avatar.png /assets/avatar/newavatar.png permanent;
Does someone know what is wrong?
If you want to use rewrite you need to use a full path to the new location
rewrite ^/assets/avatar/avatar.png http://example.com/assets/avatar/newavatar.png permanent;
A better way though is using a return
location /assets/avatar/avatar.png {
return 301 $scheme://example.com/assets/avatar/newavatar.png;
}
If you want to just rewrite, you need to change permanent to last or break

Nginx - Redirect Domain Trailing Dot

How can I redirect "http://domain.com." to "http://domain.com" with Nginx?
What's the recommended way of doing this? Regex or is there any other options?
The following snippet does this in a general way, without having to hard code any hostnames (useful if your server config handles requests for multiple domains). Add this inside any server definition that you need to.
if ($http_host ~ "\.$" ){
rewrite ^(.*) $scheme://$host$1 permanent;
}
This takes advantage of the fact (pointed out by Igor Sysoev) that $host has the trailing dot removed, while $http_host doesn't; so we can match the dot in $http_host and automatically use $host for the redirect.
You will need to use Regex.
server {
listen 80;
server_name domain.com.WHATEVER, domain.com.WHATEVER-2, domain.com.WHATEVER-3;
rewrite ^ $scheme://domain.com$request_uri? permanent;
}
From: http://wiki.nginx.org/HttpRewriteModule
redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
permanent - returns permanent redirect with code 301

Resources