Rewrite URL to only show value of $_GET argument

Andrew Martin andrew.s.martin at gmail.com
Wed Sep 11 13:32:09 UTC 2013


Francis,

Using the similar statement "try_files $uri $uri/ /index.php;", if I visit
this URL:
http://mysite.com/index.php?title=my_test_page
then the URL is rewritten to this, but it just loads the contents of
index.php (without the title variable):
http://mysite.com/my_test_page
What it shows would be equivalent to going to this page:
http://mysite.com/index.php

The part of my nginx configuration which communicates with php is:
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
        }

The php code is a custom page, not a pre-built CMS. It is doing an ajax
call to load the content, but should be functionally-equivalent to this:
<html>
<head></head>
<body>
<!-- header code here -->
<?php
if (isset($_GET['title'])) {
     include($_GET['title'] . ".html");
} else {
     include("home.html");
}
?>
<!-- footer code here -->
</body>
</html>

If I go to this page:
http://mysite.com/index.php?title=my_test_page
I would like the client's browser to instead show this URL:
http://mysite.com/my_test_page

Does this help clarify what I am looking for?

Thanks,

Andrew


On Wed, Sep 11, 2013 at 7:43 AM, Francis Daly <francis at daoine.org> wrote:

> On Tue, Sep 10, 2013 at 09:07:46PM -0500, Andrew Martin wrote:
>
> Hi there,
>
> > Would it be possible to only redirect if the title $_GET variable is
> > present?
>
> Yes.
>
> Use something like
>
>   if ($arg_title != "") {
>     return 302 http://mysite.com/$arg_title;
>   }
>
> inside the "location = /index.php" block, and then continue with whatever
> should happen if $arg_title is empty.
>
> > Thanks for clarifying this. The complete behavior I'm looking for is
> just to
> > create SEF URLs for pages on the site by hiding the index.php?title= part
> > of the URL. Thus, visiting /my_test_page in your browser would internally
> > load the index.php?title=my_test_page URL but display the SEF URL to
> > the user. How can I achieve this behavior?
>
> I suspect that "try_files $uri /$uri /index.php;" might be enough for
> what you ask for here; if it isn't, then a description of what you do,
> what you see, and what you expect to see, will probably make it easier
> to understand where the problem is. (Your fastcgi-related configuration,
> and the php code itself, will determine whether it is enough.)
>
> If you search for nginx + your-php-application, do you see any
> documentation on how to create SEF URLs? It may be easier than me
> guessing here.
>
>         f
> --
> Francis Daly        francis at daoine.org
>
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20130911/7dfa6c46/attachment.html>


More information about the nginx mailing list