Reputation: 92
I tried to look around before posting but I wasn't able to found a solution for my issue.
I used to host a blog in a domain like http://example.com/blog and I moved it to the root /
What I'd like to do is to write a rule in NGINX telling to redirect my old articles from : http://example.com/blog/my-dope-article to http://example.com/my-dope-article
Is there anyway to do that? Thanks !
I tried this : rewrite ^/blog/(.*) http://$server_name/$1 permanent; But it doesn't work because http://example.com/blog/my-dope-article still got rewritten to http://example.com/blog/my-dope-article where I want to delete "/blog" part from the new URL.. :(
Upvotes: 0
Views: 509
Reputation: 92
Yes I tried to CURL to see what happened. In the end, I found what I've done wrong: it was the syntax..
So I just put : rewrite ^/blog(.*) /$1 last; at the server directive in my nginx conf file and voilà
Upvotes: 0
Reputation: 2982
Have you looked at the redirects with curl or other tools to examine what exactly happens? If you're running wordpress on / instead of /blog/ now but haven't yet changed the home- and siteurl-options, WP will create a redirect to the URL it thinks it is running at.
When you look at the headers (e.g. with curl -I http://example.com/
or by using the developer tools in your browser), you'll usually spot differences between redirects caused by nginx and redirects caused by whatever is behind nginx.
For example, a redirect by nginx itself might look like this:
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 03 Oct 2017 18:37:06 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://www.example.org/
While a redirect from apache/php/WP behind nginx might look like this
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 03 Oct 2017 18:39:44 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: https://www.example.org/
Vary: Accept-Encoding
Content-Length: 339
Notice the moved Content-Length header, added Vary header and different Content-Type.
And, of course, you can look at the body they send with redirects as well (e.g. curl -v https://www.example.org/
).
Example for nginx:
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
apache/WP
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://www.example.org/">here</a>.</p>
<hr>
<address>Apache/2.2.16 (Debian) Server at example.org Port 80</address>
</body></html>
Upvotes: 0