0

This is a rewrite question, but it doesn't seem to be an issue with rewrites themselves--I've done all I can to try and research those. I'm moving a website from a PHP-based structure to Ghost, and I'm trying to redirect URLS in the form of example.com/blog/blog.php?postid=28 to example.com/post-title-here/. However, whenever I try to access the first URL to test a rewrite solution, it only adds a slash immediately before the ? and then proceeds to give me a 404. I've noticed it does this without any location-based rules. Is this what's preventing any rewrite solution from working? If so, how do I stop it from doing so?

example.com.conf:

server {
        listen          80;
        server_name     example.com;
        location / {
                proxy_pass http://127.0.0.1:2368;
                proxy_set_header Host $host;
        }
}
HBruijn
  • 77,029
  • 24
  • 135
  • 201
  • 1
    @KienanKnight-Boehm please include the actual contents of your configuration file(s). It is very difficult to diagnose a possible misconfiguration without those. – HBruijn Dec 15 '15 at 08:06
  • I guess, `/etc/nginx/nginx.conf` is pretty standard and we more interested in one that contains config for `example.com`. It's probably somewhere in `/etc/nginx/sites-enabled/` – Alexey Ten Dec 15 '15 at 08:25
  • @AlexeyTen I did a little research on sites-enabled and the config info has now been separated into its own file in `/etc/nginx/conf.d/`. – Kienan Knight-Boehm Dec 15 '15 at 20:20
  • @HBruijn Apologies. Added the config file to the question. – Kienan Knight-Boehm Dec 15 '15 at 20:22
  • There are no redirects shown here. What are you running on port 2368? – Michael Hampton Dec 15 '15 at 20:27
  • @MichaelHampton Ghost. The issue is that when I try to access a URL like `example.com/blog/blog.php?postid=28`, it adds a slash immediately before the question mark and then (I believe) passes it to Ghost, resulting in a 404. This happened with and without various rewrite solutions I tried, so I'm attempting to figure out what the proper way to handle rewrites is to avoid adding in the extraneous slash. – Kienan Knight-Boehm Dec 15 '15 at 20:31
  • Why do you think nginx is adding the slash? Again, there is no rule shown that would do that. – Michael Hampton Dec 15 '15 at 20:33
  • @MichaelHampton I suppose it could be Ghost as well; I'm aware there's no rule causing it. Regardless, I'm trying to figure out how a URL of the form I mentioned could be handled before even being passed to Ghost, rewritten, and _then_ passed to Ghost. Having tried many other solutions that should have worked but all ran into the slash-before-question-mark problem, I posted asking for help. – Kienan Knight-Boehm Dec 15 '15 at 21:02

1 Answers1

0

I managed to keep doing some research and answer my own question. My config file (stored in /etc/nginx/conf.d/) now looks like this:

map $query_string $updated_path {
        "postid=28"     "/proper-path-to-post/";
        "postid=27"     "/another-proper-path/";
        ...
}

server {
        listen          80;
        server_name     www.domain.*;

        location /blog/blog.php {
                rewrite ^/blog/blog.php $updated_path? permanent;
        }

        location / {
                proxy_pass http://127.0.0.1:2368;
                proxy_set_header Host $host;
        }
}

I didn't realize that nginx takes any arguments to the URL (stuff after ?) and stores it in the variable $query_string, so my earlier config was invalid. The map block does basically what it says--maps cases of $query_string to what the proper path to the post should be. Then, in the server block, the /blog/blog.php location rewrites any URLs to the proper path based on the arguments. The ? at the end of $updated_path tells nginx to not reappend the arguments passed to the URL.