问题
For some reason, I want to serve my robots.txt via a PHP script. I have setup apache so that the robots.txt file request (infact all file requests) come to a single PHP script.
The code I am using to render robots.txt is:
echo "User-agent: wget\n";
echo "Disallow: /\n";
However, it is not processing the newlines. How to server robots.txt correctly, so search engines (or any client) see it properly? Do I have to send some special headers for txt files?
EDIT 1:
Now I have the following code:
header("Content-Type: text/plain");
echo "User-agent: wget\n";
echo "Disallow: /\n";
which still does not display newlines (see http://sarcastic-quotes.com/robots.txt ).
EDIT 2:
Some people mentioned its just fine and not displayed in browser. Was just curious how does this one display correctly: http://en.wikipedia.org/robots.txt
EDIT 3:
I downloaded both mine and wikipedia's through wget, and see this:
$ file en.wikipedia.org/robots.txt
en.wikipedia.org/robots.txt: UTF-8 Unicode English text
$ file sarcastic-quotes.com/robots.txt
sarcastic-quotes.com/robots.txt: ASCII text
FINAL SUMMARY:
Main issue was I was not setting the header. However, there is another internal bug, which is making the Content-Type as html. (this is because my request is actually served through an internal proxy but thats another issue).
Some comments that browsers don't display newline were only half-correct -> modern browsers correctly display newline if content-type is text/plain. I am selecting the answer that closely matched the real problem and was void of the above slightly misleading misconception :). Thanks everyone for the help and your time!
thanks
JP
回答1:
Yes, you forgot to set the Content Type of your output to text/plain
:
header("Content-Type: text/plain");
Your output is probably being sent as HTML, where a newline is truncated into a space, and to actually display a newline, you would need the <br />
tag.
回答2:
header('Content-Type: text/plain')
is correct.- You must call this method before anything is written to your output, including white space. Check for whitespace before your opening
<?php
. - If your
Content-Type
header has been set totext/plain
, no browser in its right mind would collapse whitespace. That behaviour is exclusive to HTML and similar formats. - I'm sure you have your reasons, but as a rule, serving static content through PHP uses unnecessary server resources. Every hit to PHP is typically a new process spawn and a few megs of memory. You can use apache config directives to point to different robots files based on headers like
User-Agent
- I'd be looking into that. - It's likely that search engines ignore the
Content-Type
header, so this shouldn't be an issue anyway.
Hope this helps.
-n
回答3:
<?php header("Content-Type: text/plain"); ?>
User-agent: wget
Disallow: /
BTW, the newlines are there just fine. They're just not displayed in a browser. Browsers collapse all whitespace, including newlines, to a single space.
deceze$ curl http://sarcastic-quotes.com/robots.txt
User-agent: wget
Disallow: /
回答4:
You must set the content type of the document you are serving. In the case of a .txt text file:
header("Content-Type: text/plain");
The IANA has information about some of the more popular MIME (content) types.
回答5:
i was having a similar issue and either "\n" nor PHP_EOL worked. I finally used:
header('Content-Disposition: attachment; filename="plaintext.txt"');
header("Content-Type: text/plain");
echo "some data";
echo chr(13).chr(10);
The echo of BOTH characters did the trick. Hope it helps someone.
Bye anankin
回答6:
If you are using echo, then use <br>
for new lines. the printf function is what uses \n.
In your case, use printf because you are not using HTML. I believe this is the proper way to do this, along with setting the MIME type to text.
来源:https://stackoverflow.com/questions/4506679/rendering-plain-text-through-php