-2

I'm using a php script to send users from mysite.com/id=123 to mysite.com/my-id-title

I could use .htaccess file to rewrite the URL (which is -of course the natural solution- but given that the replacement information for the url is not in the original parameter provided by the url, but requires a database query (as you can see in the example) I cannot do that with just mod_rewrite. And to make matters more difficult, I do not have access to Apache, I can just use the .htaccess.

So, I thought, as a workaround, to generate a rule though php, which does work using header('Location:'.$reenvioA);

The problem is that PHP generates a 302 response code, instead of 200. A 200 response is what I need to create a sitemap and for SEO reasons.

enter image description here

So, in order to prevent that, I thought about telling php that I want a 200 code, so I force it.

I've tried quite a few ways:

header("HTTP/1.1 200 OK");
header('Location:'.$reenvioA);

Maybe forcing it before and after the Location header?

header("HTTP/1.1 200 OK");
header('Location:'.$reenvioA);
header("HTTP/1.1 200 OK");

And then I've tried doing it using their optional arguments:

header('Location:'.$reenvioA, false, 200);

Maybe just letting the second argument remain true?

header('Location:'.$reenvioA, true, 200);

And then just setting the variable with the response itself:

header('Location:'.$reenvioA);
http_response_code(200);

So far, none has worked, because as soon as I forced the response, the page won't load. It seems that php won't fetch the page, even when the response code is set after header(Location:url). enter image description here

What can I do?

Rosamunda
  • 13,549
  • 8
  • 31
  • 60
  • And why exactly are you insisting on a 200 response code…? – deceze Aug 13 '19 at 13:14
  • You put it on hold because you don't understand _why_ I want to have a 200 response code? Because **in my opinion the question is very clear: I want to have a 200 response code** (for SEO reasons would be the explanation about my _inner motivations_, which has nothing to do with a programming question). You set my question on hold because you think that's "unclear what I'm asking" but it is NOT unclear. You just want to know _why_ I would want such a thing, which is another matter. – Rosamunda Aug 13 '19 at 16:55
  • 4
    Well, you either want to redirect, or you want a 200 response code. You can’t have both. So it’s *unclear* what you want to achieve with this. – deceze Aug 13 '19 at 18:00
  • 1
    Okay, so the other way around then: why are you setting that Location header if you *don’t* want to redirect? – deceze Aug 13 '19 at 18:41
  • I want to rewrite all URLs. For that, I need a more complicated rewrite rule than the one that I can set in my .htaccess file. I cannot do that without rewriteMap, but I don't have access to Apache So, I tried a workaround. I thought I could achieve that changing the header... If you have another solution please suggest it. **Until now, to my very clear question, I've got one, real, usable, suggestion: try it with JS.** Do you have any other suggestion? Maybe someone else can suggest something else and help me out if the "unclear" flag weren't there. – Rosamunda Aug 13 '19 at 18:58
  • A "location" header always imply a redirection. Doesn't make sense and it's not possible to return a status 200 to a server redirection. Using "location" headers or any other kind of redirection for human-readable links is a very bad solution. Bad for users, bad for SEO, bad for you. – yivi Aug 13 '19 at 19:05
  • So, frankly, this question is nonsense as is. Using JS is also nonsense. You want pretty URLs. Currently you have non-pretty URLs, and you want to make them pretty. Read this: https://stackoverflow.com/a/20563773/476. You need to use pretty URLs first and foremost. You don’t want to rewrite or redirect non-pretty URLs. That is, *as far as I could gather from the little bit of information you offer.* If your situation is somehow different, explain more. – deceze Aug 13 '19 at 19:06
  • Thanks deceze. Now I understand what you actually mean with "unclear" (My natural language is not english). Hopefully the question is better explained now? – Rosamunda Aug 13 '19 at 19:21
  • Yeah, no, it’s not. Your visitors visit a non-pretty URL. You want to redirect them to a pretty URL (‽). For this you necessarily need to respond with a 3xx code. Anything else still makes little sense. – deceze Aug 13 '19 at 19:24
  • You do not need to do any kind of redirection. This is standard URL rewriting. You rewrite all incoming hits to a single script, and that script is in charge of actually providing the content (using whatever resources and controller your application has available). No redirection. No "location" headers. Just search for "PHP rewrite URLs pretty links". – yivi Aug 13 '19 at 19:49
  • "_For this you necessarily need to respond with a 3xx code_" I've read that you need not to redirect the url but to rewrite it. Hence my problem trying to rewrite it (and not having two duplicate addresses for the same thing, the ugly and the pretty). How do you do that when you need to bring stuff from database after doing a query in order to generate the pretty link while not having access to apache? Again, thanks and sorry for the misunderstanding. – Rosamunda Aug 13 '19 at 21:41
  • Read again my aforelinked post on what *rewriting* is. It means you request the pretty URL and make the web server handle it correctly by rewriting the request *internally*. This may or may not be necessary for you to handle pretty URLs on your server. Now, if you have *ugly* URLs for some reason (why?) that your visitors are visiting that you want to *redirect* to the pretty equivalent, you necessarily need to issue a *redirect response*, which is a 3xx code with a Location header. – deceze Aug 14 '19 at 05:05

1 Answers1

5

You can't do that, since you are redirecting you can only use 3xx response codes, any response code not in the 3xx range will not work.

(If its a option you can use JS to redirect and get a 200 without changing any headers)

Moses Schwartz
  • 1,938
  • 1
  • 17
  • 28