Nginx: Rewrite URL to hash - nginx

I have an old site which has been upgraded to an SPA frontend, so now the URLs to access certain records use the hash, so the URLs changed from like example.com/aq/12345 => example.com/#/aq/12345
This is already working fine, but I also wanted to set up a redirect so that anyone who had the old URLs would be redirected to the new URLs. I tried setting up the rewrite like this:
location / {
root /var/www/frontend;
autoindex on;
rewrite ^/aq/(.*)/$ /#/aq/$1/ last;
}
... but when I did this, nginx simply started looking for a file named /var/www/frontend/#/aq/[whatever]/index.html, instead of simply redirecting to /var/www/frontend/index.html with the hash in the URL. What is the proper way to configure this, or is not possible to do this at all?

The proper way is to use the HTTP 301 status code:
location /aq {
root /var/www/frontend;
autoindex on;
return 301 https://example.com/#$request_uri;
}

Related

Nginx, nuxt.js static generated mode and redirection of trailing slashs

i currently have a problem configuring my Nginx correctly for nuxt.js generated sites.
What i want to achieve is the following
/magazin -> /magazin/index.html
/magazin/ -> 301 /magazin
/magazin/artikel/titel-goes-here -> /magazin/artikel/titel-goes-here/index.html
/magazin/artikel/titel-goes-here/ -> 301 /magazin/artikel/titel-goes-here
currently this is the other way around.
If im correct i shouldn't use a proxy pass to a e.g. pm2 instance with express etc. as it destroys the sense of static site generation.
But how can i get this page structure to work, as i need the same url's as our legacy service for SEO reasons, which used Angular Universal SSR
my current config is:
location ^~ /magazin {
root /path/to/dist;
index index.html ;
}
if i add something like
rewrite ^(.+)/+$ $1 permanent;
i get an infinite 301 loop
Thanks for the help
You cannot use the built in index directive, as it works the other way around (as you have observed).
You can use try_files to test for the existence of the index.html file. Use a named location to process the redirection.
For example:
location ^~ /magazin {
root /path/to/dist;
try_files $uri $uri/index.html #rewrite;
}
location #rewrite {
rewrite ^(.+)/$ $1 permanent;
}
See this document for details.

How do I configure nginx for WordPress REST API in sub-folder?

I am trying to set up multiple Wordpress sites in sub-folders under our domain (ie not multi-site), but I have difficulty configuring the REST API endpoints. For example, this endpoint works fine:
https://example.com/site1/?rest_route=/wp/v2/posts
But this endpoint gives a 404:
https://example.com/site1/wp-json/wp/v2/posts
I have tried to rewrite the failing url to the working url with these rules in my nginx configuration:
location /site1/wp-json {
rewrite ^/site1/wp-json(.*)$ /site1/?rest_route=$1;
}
location /site1/ {
try_files $uri $uri/ /site1/index.php$is_args$args;
}
I can't see any special handling of wp-json in the WordPress docs or the nginx wiki. What am I missing here? The permalinks for the site is set to Numeric (https://example.com/site1/archives/123) if that might play a role.
Update
Gist of the redacted full config file and the config syntax lints okay:
nginx -c /etc/nginx/nginx.conf -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
I just hit this too, in WP 5.7. Without pretty permalinks, ie with the "Plain" option like ?p=123, my nginx WP installation uses requests like:
/index.php?rest_route=/wp/v2/users/&who=authors...
And these all work fine.
However if I enable pretty permalinks, eg "Post name", /sample-post/, it starts making requests like:
/wp-json/wp/v2/users/?who=authors...
And these all return a 404. For example, editing or publishing posts fails, and browser devtools shows a string of 404s in this format.
But now we know the pattern that works, a solution is clear - we just need to map the not-working format to the working format:
# Resolves WP Gutenberg 404 issue
location /wp-json {
rewrite ^/wp-json(.*)$ /index.php?rest_route=$1 last;
}
I believe that the rewrite directive should be written as shown below:
server {
location /site1/wp-json
{
rewrite ^(/site1/wp-json.*)$ /site1/?rest_route=$1 last;
}
}
I was able to resolve it like this:
location /wordpress/ {
rewrite ^/wordpress/wp-json/(.*?)$ /wordpress/index.php?rest_route=/$1 last;
}
An easy way if your website pages in the subfolder is already working, just add index.php to the url:
https://site1.com/site2/index.php/wp-json/
If your website pages still doesn't work in the subfolder, add this code to nginx/sites-available/website.conf file too:
location /site2 {
rewrite ^(/[^/]+)?(/wp-.*) /site2/$2 break;
rewrite ^/site2/(.*)$ /site2/index.php?q=$1 last;
}

NGINX Subdirectory Redirect is not working

TLDR: subdirectory/cool redirects to newdirectory/cool. subdirectory?cool incorrectly redirects to newdirectory without the parameter.
The Prob:
I'm attempting to redirect a subdirectory to a new directory while preserving the original URL parameters via NGINX location using the regex ~* ^(?:((\/m\b)(?!-)(\/)?))(?<requestedPath>.*)?$
The paths such as /subdirectory/cool successfully redirect to /newdir/cool. However, paths like /subdirectory?cool and /subdirectory/?cool are redirecting to /newdir without preserving the URL parameters.
The redirect should work as follows:
/subdirectory -> /newdir
(Many of the suggestions I've seen posted here for subdirectrory redirects assume a trailing slas or do not take into account that their regex for /m also impacts /media)
/subdirectory/ -> /newdir/
/subdirectory/test -> /newdir/test
/subdirectory/?apple -> /newdir/?apple
/subdirectory?apple -> /newdir?apple
The test is successful here: Successful Test
The code that is failing:
location ~* ^(?:((\/subdirectory\b)(?!-)(\/)?))(?<requestedPath>.*)?$ {
return 301 /newdir/$requestedPath;
}
Additionally, I have also tried using rewrite instead of location to no avail.
Apparently, with location, one has to us $is_args.
This did the trick: return 301 /newdir/$requestedPath$is_args$args;
End result:
location ~* ^(?:((\/subdirectory\b)(?!-)(\/)?))(?<requestedPath>.*)?$ {
return 301 /newdir/$requestedPath$is_args$args;
}

nginx substitude file sitemap.xml on subdomain

I have file sitemap.xml in site public directory. When I use subdomain 'm.', it uses same public directory as general site. I need to return file '/mobile-sitemap.xml' from url '/sitemap.xml', when I'm on subdomain 'm.'.
What nginx modules and rules could help me to do this?
My first step was like this:
if ($host ~* m\.(.*)) {
#if url = /sitemap.xml, give back file /mobile-sitemap.xml as /sitemap.xml
}
Or maybe it's ok to google, if i just send 301 redirect to another sitemap file?
You could create a separate server block for your mobile subdomain, in which case the if statement would not be necessary. See this caution on the use of if.
However, to implement this in a single server block, use a location and a rewrite:
location = /sitemap.xml {
if ($host ~* m\.) {
rewrite ^ /mobile-sitemap.xml last;
}
}
See this and this for more.

nginx 301 rewrite root domain works but files are not being redirected

I'm trying to redirect a domain to another url in nginx and it works partially.
It looks like this:
location / {
rewrite ^/(.*) http://www.some_domain.tld/some_dir/some_file.php permanent;
}
It redirects fine the root domain, however if I try to load http://www.my_old_domain.tld/some_file.php it won't do the same. It'll load the page.
Thoughts ?
Thanks.
Try using this
location /
return 301 http://newdomain.com/a/b/c.php;
}

Resources