1

i need to embed a html 301 redirect in my php code and i wanted to save chars in my html. Can i write HTML on one line since it is parsed? Is there anything against this practice?

?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>301 Moved Permanently</title></head><body><h1>Moved Permanently</h1><p>The document has moved <a href="<?php echo $location ?>">here</a>.</p></body></html>
<?

thank you

2
  • 2
    Yes, a lot of sites run their html, css & javascript through minimizers that remove unnecessary whitespace (and do other optimizations). The result looks mostly like this. Though I don't see why you would care about those few bytes in this specific case. Mar 13, 2015 at 8:18
  • 1
    Premature optimization :) Mar 13, 2015 at 8:26

2 Answers 2

1

Your single-line HTML snippet will work fine. Improperly written browsers could conceivably choke on one-liners that exceed ridiculous maximum lengths, but in practice something like your snippet should not pose any problem.

Of course, since you work in PHP anyway, my preferred way of handling redirections would be something like:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.New-Website.com");
?>

But that's up to you. :-)

0

The general practice is to redirect to an error page whose name is based on the type of error code, for instance a 301 redirect would be called 301.html. You can pass url parameters for the location and you could use a small javascript to extract these parameter values. The reason why you should generally use html rather than php is that html is more robust, and should there be serious problems with the server, at least if the server can still serve static files, the errors should still work as expected.

If readability is your issue, putting this page in its own file is as best as you can hope for. You can fully expand the html in its own page, so it is readable, yet it is also equally functional. If optimization is your concern, this is not the approach you should take. There are plenty of libraries in php that will compact these files for you. In the long run, it is not worth it to you to try to squeeze everything into one line, even for a page that isn't likely to change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.