Forums

Give help. Get help.

  • # May 12, 2013 at 5:55 am

    Let’s say I have a website : example.com

    And a webpage: example.com/page.php

    I want to make it more friendly, so I use .htaccess: example.com/page

    Which contains links to other “pages”. They are actually the same one, the only difference being the GET parameters from the URL, which would pull different content from the database.

    So by clicking the links within “page.php”, wether you’re on “example.com/page.php” or “example.com/page”, you’ll get to “example.com/page.php?category=design&page=3&lang=ro”

    I want to make it more friendly too, so I use, again, .htaccess. The result would be “example.com/page”, followed by all those values, separated by slashes: “example.com/page/design/3/ro”

    My question is: Within the page.php file, how on earth am I going to be able to make use of the GET parameters??

    $_GET[“category”] or $_GET[“page”] wouldn’t work, at least in my case. Does it work for you? It isn’t even logical to work. There is no “category” or “page” parameter in the URL. So when attempting to write $_GET[“category”], the server will analize the URL and it will need the actual parameter NAME as well as the VALUE. In our case, we only have the value. Someone, please fill me in?

    # May 12, 2013 at 6:48 am

    There are some parameters whose names I don’t want to show off in the URL. Anyway, I didn’t even know that that was possible! I should have thought of it…Thanks! Even though, I will not be able to use “isset” or “$_GET”, will I?

    # May 12, 2013 at 6:21 pm

    So you’re saying that in the above example, while the browser will show the user:
    http://example.com/page/design/3

    It will actually be able to detect wether a parameter is set or not? Like:
    if(isset($_GET[“category”]))

    Even though we do not have the real parameter name?

    The reason for this being that, in the “back”, the users are actually on:
    http://example.com/page.php?category=design&page=3
    ?

    And regarding your personal choice: What do you mean, more exactly, by “redirect everything through index.php”?

    __
    # May 12, 2013 at 7:41 pm

    Here’s what I do – a very similar approach:

    `.htaccess`

    # if the filename is a “real” file or directory, don’t rewrite.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # if not, take the entire URL + query string and append it to index.php.
    RewriteRule ^.*$ index.php%{REQUEST_URI} [QSA,L]

    In php, you can get the original path the user typed with `$_SERVER` (or, on some hosts, `$_SERVER`), and the query string with `$_SERVER` (or, simply use the `$_GET` superglobal like normal).

    The difference between using `PATH_INFO` and `REQUEST_URI` (as @benwalker suggests) is that the latter will return both the path info and the query string together (and there’s nothing wrong with that; I just find it less useful). `REQUEST_URI` will still be available to you regardless of how you rewrite the request.

    < ?php
    # example: user typed “example.com/pretty/url?ugly=query string”

    print $_SERVER;
    # prints “/pretty/url?ugly=query%20string”

    print $_SERVER;
    # prints “/pretty/url”

    print $_SERVER;
    # prints “ugly=query%20string”

    print $_GET;
    # prints “query string”

    # May 16, 2013 at 9:00 am

    I don’t understand. Why would you want to rewrite everything to “index.php”? Alright, as a programmer, I will still be able to get those parameters and their values. But as a user, whatever I would type, whether it’s example.com/page or example.com/page1 or example.com/page5/design I will be redirected to example.com/index.php

    # May 16, 2013 at 9:09 am

    The request go trough index.php but the user won’t see that. It’s an easy way to make pretty urls. You only have one place in which you need to find out what the requested page was and then serve that page.

    # May 16, 2013 at 9:31 am

    > You only have one place in which you need to find out what the requested page was and then serve that page.

    So I will analize the requested page right in “index.php”, and I will redirect the user to that specific page?

    # May 17, 2013 at 2:26 am

    > This is because my URLs only include the module name in the path – everything else is in the query string.

    And, in your case, what do you use to get the query string? Simply GET or SERVER variables?

    # May 17, 2013 at 5:22 pm

    explode() is what I was thinking about too.

    Sorry for asking if you use server variables..I know the whole point of your previous post was to clarify that. I just don’t know what I was thinking of.

    Anyway, thank you for your prompt and helping responses!
    I have definitely learned something from you.
    I’ll use your suggestions and maybe implement your techniques in future projects.

    Have a great day!

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag