Forum Moderators: coopster

Message Too Old, No Replies

Transfering / redirecting page variables in PHP

         

asusplay

12:45 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



Hi, I am a bit of a PHP newbie and I am currently translating my site form classic ASP to PHP, which has been a steep learning curve but I am getting to grips with it. However I have now hit a stumbling block and I am not sure how to solve this.

This relates to the news articles section of my website. The way that I do this is that I add a news article into the database (formerly Access, now Mysql) so that news articles appear as (example) domainname/news/317/

317 is the id of the news article. There is no folder with this name so the way it used to be handled in classic asp was that it would trigger a 404 error and in the error page i had a server.transfer function, which directed it to a news.asp page which would then execute the display of the news article. To the user and search engine robots it still looked like "/news/317" because the server.trasfer method keeps the URL the same and allows you to transfer the query string and form variables.

My question is, can i do the same thing with php? Is there a simpler way? And if so, what is the method for doing this? Right now I don't even know where to look.

Thanks!


'example code used in classic asp

Dim strQuerystring, aParameters
strQuerystring = Mid(Request.ServerVariables("QUERY_STRING"),12)
aParameters = Split(strQuerystring,"/")

On Error Resume Next

Server.Transfer(aParameters(1) & ".asp")

If Err Then
Response.Status = "404 Not Found"
Server.Transfer("error-message.asp")
End If

IanTurner

12:50 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Now that is a coincidence - I was just about to ask a very similar question.

Is there a server.transfer equivalent in PHP?

Anyango

2:18 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are on an Apache server, mod_rewrite is used for this. It will simply require an expression in a .htaccess file and thats it, you can achieve what you are looking for right away. Please have a look at apache forum, every other thread there will talk of that. or search for "mod rewrite static urls"

IntegrityWebDev

2:20 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



I'm not exactly sure if I understand the problem but you may look at the PHP header statement

[php.net ]

**Edit: Nevermind...see the mod rewrite post and now I understand what you were needing. :-)

IanTurner

2:43 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hmm - was looking more at a situation whereby I do some processing first and then transfer to a second file based on that initial processing.

Not sure that mod_rewrite is capable of that.

asusplay

2:48 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



I am on a windows (shared) server and I'm not sure I have .htacess, as far as I can see there isn't a .htaccess file on the root of my domain. Is there a way to do this with using mod rewrite then?

thanks

rocknbil

5:04 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



was that it would trigger a 404 error and in the error page i had a server.transfer function,


Doesn't matter what server you use this is a bad idea on any server.

To the user and search engine robots it still looked like "/news/317"


This has to be false, what does Webmaster tools say? A 404 issues a 404 not found header.

A 404 is still a 404 to the search engines, even though your users get the document. The right way is to form some pattern on the request and capture it, at the very least, do a file system check to see if the file exists (also bad, not as bad) - but never as a reaction to a 404. (I learned the hard way.) So now that you're in it new, do it right. :-)

I am too rusty on Windows machines, but yes, the equivalent of a rewrite is what you want, and it can be done. You don't want to do it at the script level, that would at the very least involve a redirect, you want it at the server level.

Right now I don't even know where to look.


The folks in the Windows Servers Forum [webmasterworld.com] will help you out, ask about a windows based rewrite.

IanTurner

6:01 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



rocknbil, you can send any header you like using ASP, you just change the 404 to a 200 okay response in the code.

asusplay

9:35 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



rocknbil, yes a 404 is a bad idea but it was a fix to what i needed to do at the time and in ASP but being on a windows shared server with a rather inflexible web host it was the only option.

The workaround that I have done is:

Get the ID of the news article from the address being passed (so in /news/317 it would be 317) and extract the information from the database using the news ID and display it in a news article template file.

However when I add a news article to the db, it also creates a folder in the /news/ directory with the same name as the news ID created (in this case 317) using the mkdir() function and copy into it the news article template file using the copy() function. Therefore there is actually a folder and file so the address requested corresponds to an actual path. Might not be perfect and a bit simplistic but works for me :)

rocknbil

11:20 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you can send any header you like using ASP


Of course you can, and any other language. But the server has already responded with a 404 before it gets to the script. Right?

Request -> 404 -> on 404 -> script -> 200 OK.

IanTurner

2:20 am on Jun 26, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Of course you can, and any other language. But the server has already responded with a 404 before it gets to the script. Right?


No if you set the Custom Error for 404 to be a script it will run the script before sending the header.

Request -> script -> 200 OK with output

coopster

10:24 am on Jun 26, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can do the very same with PHP. The first thing that must happen is that the HTTP server, whether it is Apache or IIS, must recognize the resource at it's script level. With Apache you can accomplish this in a number of ways, mod_rewrite being but one. Content negotiation using variants or multiviews is another. However it is done, proper configuration at the HTTP server level is the key.

I think rocknbil is saying that if really intend to send a 404, send the appropriate 404 header and dump the content. PHP can handle this, yes. What you don't want to do is send a 404 header and then redirect to your content because the redirect header will be used (302 by default), ultimately delivering 200 OK content to the requester. I've seen this practice used and it can cause issues with indexing in certain search engines. Google calls them Soft 404s: Crawl errors now report soft 404s [googlewebmastercentral.blogspot.com]

rocknbil

6:57 pm on Jun 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No if you set the Custom Error for 404 to be a script it will run the script before sending the header.


Is this specific to Windows servers? Odd . . . I've always seen the header first, then the content, which is why they call it header, I think. A 404 header is sent first, then a 200 on the 404 "page" (script.) I first encountered this problem because . . . I used to do it, used the 404 document as the dynamic script. Turned out to be very bad. :-(

g1smd

9:26 pm on Jun 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There is no folder with this name so the way it used to be handled in classic asp was that it would trigger a 404 error and in the error page i had a server.transfer function, which directed it to a news.asp page which would then execute the display of the news article.

The above has to rank among the most botched ways of doing this stuff.

In Apache you have "Redirects" and "Rewrites", and in PHP you have the "Header" directive and the "Includes" functionality. With these tools you can do almost anything. :) Make sure that you are very clear as to what they each do.

penders

1:14 am on Jun 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you are on a shared Windows server (probably IIS?), with no access to .htaccess (no mod_rewrite) then I guess a custom 404 script (as you have done with ASP) may be the (only) way to go?!

I was installing a site on a shared Windows IIS recently and I didn't even appear to have access to a custom 404 error page(?!) - you're lucky!

IanTurner

2:35 pm on Jun 28, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



No if you set the Custom Error for 404 to be a script it will run the script before sending the header.

Is this specific to Windows servers?


That is what I was trying to find out - I can do this with IIS 6 but haven't found a way of doing it with Apache yet (I have a Windows server background and my Apache/PHP is not good yet)

penders

1:24 pm on Jun 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That is what I was trying to find out - I can do this with IIS 6 but haven't found a way of doing it with Apache yet


You don't need to (although you can, but probably shouldn't) do this with Apache, as mentioned above. You can set up redirects and rewrites in .htaccess

To set up custom 404 pages you can use the ErrorDocument directive in .htaccess which directs all 404's (or whatever error you choose) to a page of your choice.