-2

I'm a bit confused and I need help to finish a project, so thank you for your answers. I will not post my code because its very messy but I'll use an example:

Let's assume I have a url like this:

http://localhost/mySite?q=bar&g=foo

And let's assume I have a rewrite rule like this:

RewriteRule ^mySite/bookingConfirmed$ mySite.php?q=$1&g=$2

First question: If I have link with href=mySite?q=bar&g=foo and click on it, the url showed in the reached page will be like the rewrote one or it will still be like it was written in the href?

Second question: If I have a php code like this:

$q = $_GET['q']; 

Does $q will read the q parameter if I have a rewrote url?

Thank you all again.

EDIT:

I'm asking this because I have a very dirty url coming from the server from which I take some data of a just recorded booking (like the day, the client name ecc). I need this data to start a little script, which read them from a input type hidden.

Rilay
  • 11
  • 2
  • 1
    _"And let's assume I have a rewrite rule like this"_ - that pattern makes _very_ little sense. You are demanding "zero or more slashes", then a slash, and then "zero or more slashes" again. Not very likely that you will actually encounter URLs that have multiple consecutive slashes, and your pattern does not allow for anything else _but_ slashes either. – CBroe Mar 10 '22 at 14:42
  • 1
    _"the url showed in the reached page will be like the rewrote one or it will still be like it was written in the href?"_ - if you are only making an _internal rewrite_, then the browser address bar will still show the original URL, as it was contained in the links `href`. If you make an _external redirect_ - either by specifying the `R` flag, or by giving an absolute substitution URL with a different hostname (or protocol) - then the browser will make a new request for the new URL, and show the new URL in the address bar as well. – CBroe Mar 10 '22 at 14:44

1 Answers1

1

Rewrite rules, in their default mode, take the URL sent by the browser, and pretend that a different URL was requested instead.

For instance, if you have the rule:

RewriteRule (.*)/(.*) example.php?a=$1&b=$2

If you type https://example.com/foo/bar into the browser, the rule will transform that to /example.php?a=foo&b=bar. As far as the browser is concerned, nothing has happened; as far as PHP is concerned, https://example.com/example.php?a=foo&b=bar is the URL that was requested, and everything like $_GET works exactly as normal.

Note that it is generally a mistake to think of rewrite rules as "making URLs pretty". The input is the pretty URL, and the rewrite rule tells the server how to interpret that URL. You can't use a rewrite rule to "hide" parameters, because if the browser doesn't send them, you can't see them.

There are a couple of relevant details:

  • If you add the [R] flag to a rule, it does tell the browser to request the new URL, instead of pretending that's what was requested right now. This also happens automatically if you give an absolute URL as the target (starting http:// or https://) rather than a relative one.
  • If you have a URL like https://example.com/foo/bar?name=bob, you can add the [QSA] flag to a rule to copy the name=bob parameter onto the rewritten URL, giving in this example /example.php?a=foo&b=bar&name=bob

For more details and further links, see Reference: mod_rewrite, URL rewriting and "pretty links" explained

MrWhite
  • 43,179
  • 8
  • 60
  • 84
IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Does this work even in reverse? If I have a php redirect from the server to "http://example.com/page.php?q=foo" how can I show in the url bar just "http://example.com/page/foo" or maybe even (this is what I need for my website) "http://example.com/page"? Thank you for the answers so far – Rilay Mar 10 '22 at 14:54
  • @Rilay Every request *starts* at the browser, all you can do is *respond*. You can look at the URL the browser sent, and decide what to do: serve a particular page, or tell the browser to load a different URL instead. You can certainly have a rule that matches `https://exmaple.com/page`, but you can't make that *sometimes* mean `https://exmaple.com/page.php?q=foo` and *sometimes* `https://exmaple.com/page.php?q=bar`, because you only have what the browser sent you. – IMSoP Mar 10 '22 at 15:04
  • so this line won't work? `^home/invioEseguito$ index.php?mode=invioEseguito&numero=$1&nome=$2 [R]` – Rilay Mar 10 '22 at 15:35
  • @Rilay No, because `$1` and `$2` have to come from somewhere; the rewrite rule can't just invent them. – IMSoP Mar 10 '22 at 15:53
  • @Rilay [This answer I wrote on the linked reference](https://stackoverflow.com/a/67254596/157957) is particularly relevant. – IMSoP Mar 10 '22 at 16:16
  • @IMSoP "Rewrite rules, in their default mode" - What do you mean by "default mode"? What _other mode_ is there? "if you have the rule:" - your example rule does not actually match your example URL in a _directory_ context (and matches "incorrectly" in a _server_ context). – MrWhite Mar 11 '22 at 19:00
  • 1
    @MrWhite By "default mode", I mean without flags like `[R]` and `[F]` which generate an HTTP response directly, rather than actually rewriting the request. The example rule was written in a bit of a hurry (originally I had just *described* the rule, but thought a concrete example would be easier to read), and I admit I'm much more familiar with configuring in a `` block than in a `.htaccess` file, so I'm not surprised if I've got the details wrong; feel free to edit it to be more accurate. – IMSoP Mar 11 '22 at 19:10
  • @MrWhite I'm kind of tempted to just mark the question as a duplicate of the reference one anyway, since this answer ended up mostly repeating things that are already said there. – IMSoP Mar 11 '22 at 19:11
  • @IMSoP I updated the regex for a minimal working solution (in `.htaccess` at least) to get the point across. Although in practise, something like `^/?([^/]+)/([^/]+)$` might be more accurate. Nice explanation +1 – MrWhite Mar 11 '22 at 19:45