Skip to main content
added 34 characters in body
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51

First of all I would get rid of that page thing. Your web-server already provides the requested resource, through REQUEST_URI variable. No need to pollute the query string with a useless variable that could be easily overwritten. So make your rule just RewriteRule ^([a-z_-]+)$ index.php [L,QSA] (the original query string is a goofgood thing to keep, so we're adding QSA)

$page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$page = trim($page, "/"); // original request may have them
if (in_array($page, ['index.php', 'home'], true)) {
    header("Location: /", true, 301);
    die; // always die if there is any code after Location:
}
$page = $page ?: 'home'; // make it home if empty

if (preg_match('~^[a-z_-]+$~', $page) && file_exists("inc/$page.php")) {
    require "inc/$page.php";
    include "tpl/$page.tpl.php";
} else {
    http_response_code(404);
}

HTTP status codes are not for decoration. They must be used in the exact situations they are intended for. When a page is not found, your server should respond with 404 status, not 302. I would advise to create designatedcorresponding pages (i.e. 404.php, 500.php) in the inc directory and include them along with setting appropriate HTTP status instead of redirect.

First of all I would get rid of that page thing. Your web-server already provides the requested resource, through REQUEST_URI variable. No need to pollute the query string with a useless variable that could be easily overwritten. So make your rule just RewriteRule ^([a-z_-]+)$ index.php [L,QSA] (the original query string is a goof thing to keep, so we're adding QSA)

$page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$page = trim($page, "/");
if (in_array($page, ['index.php', 'home'], true)) {
    header("Location: /", true, 301);
    die; // always die if there is any code after Location:
}
$page = $page ?: 'home'; // make it home if empty

if (preg_match('~^[a-z_-]+$~', $page) && file_exists("inc/$page.php")) {
    require "inc/$page.php";
    include "tpl/$page.tpl.php";
} else {
    http_response_code(404);
}

HTTP status codes are not for decoration. They must be used in the exact situations they are intended for. When a page is not found, your server should respond with 404 status, not 302. I would advise to create designated pages in the inc directory and include them along with setting appropriate HTTP status.

First of all I would get rid of that page thing. Your web-server already provides the requested resource, through REQUEST_URI variable. No need to pollute the query string with a useless variable that could be easily overwritten. So make your rule just RewriteRule ^([a-z_-]+)$ index.php [L,QSA] (the original query string is a good thing to keep, so we're adding QSA)

$page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$page = trim($page, "/"); // original request may have them
if (in_array($page, ['index.php', 'home'], true)) {
    header("Location: /", true, 301);
    die; // always die if there is any code after Location:
}
$page = $page ?: 'home'; // make it home if empty

if (preg_match('~^[a-z_-]+$~', $page) && file_exists("inc/$page.php")) {
    require "inc/$page.php";
    include "tpl/$page.tpl.php";
} else {
    http_response_code(404);
}

HTTP status codes are not for decoration. They must be used in the exact situations they are intended for. When a page is not found, your server should respond with 404 status, not 302. I would advise to create corresponding pages (i.e. 404.php, 500.php) in the inc directory and include them along with setting appropriate HTTP status instead of redirect.

added 21 characters in body
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51

First of all I would get rid of that page thing. Your web-server already provides the requested resource, through REQUEST_URI variable. No need to pollute the query string with a useless variable that could be easily overwritten. So make your rule just RewriteRule ^([a-z_-]+)$ index.php [L,QSA] [L]`(the original query string is a goof thing to keep, so we're adding QSA)

$page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$page = trim($page, "/");
if (in_array($page, ['index.php', 'home'], true)) {
    header("Location: /", true, 301);
    die; // always die if there is any code after Location:
}
$page = $page ?: 'home'; // make it home if empty

if (preg_match('~^[a-z_-]+$~', $page) && file_exists("inc/$page.php")) {
    require "inc/$page.php";
    include "tpl/$page.tpl.php";
} else {
    http_response_code(404);
}

First of all I would get rid of that page thing. Your web-server already provides the requested resource, through REQUEST_URI variable. No need to pollute the query string with a useless variable that could be easily overwritten. So make your rule just RewriteRule ^([a-z_-]+)$ index.php [L]`

$page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$page = trim($page, "/");
if (in_array($page, ['index.php', 'home'], true)) {
    header("Location: /", true, 301);
    die; // always die after Location:
}
$page = $page ?: 'home'; // make it home if empty

if (preg_match('~^[a-z_-]+$~', $page) && file_exists("inc/$page.php")) {
    require "inc/$page.php";
    include "tpl/$page.tpl.php";
} else {
    http_response_code(404);
}

First of all I would get rid of that page thing. Your web-server already provides the requested resource, through REQUEST_URI variable. No need to pollute the query string with a useless variable that could be easily overwritten. So make your rule just RewriteRule ^([a-z_-]+)$ index.php [L,QSA] (the original query string is a goof thing to keep, so we're adding QSA)

$page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$page = trim($page, "/");
if (in_array($page, ['index.php', 'home'], true)) {
    header("Location: /", true, 301);
    die; // always die if there is any code after Location:
}
$page = $page ?: 'home'; // make it home if empty

if (preg_match('~^[a-z_-]+$~', $page) && file_exists("inc/$page.php")) {
    require "inc/$page.php";
    include "tpl/$page.tpl.php";
} else {
    http_response_code(404);
}
added 830 characters in body
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51
$page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$page = trim($page, "/");
if (in_array($page, ['index.php', 'home'], true)) {
    header("Location: /", true, 301);
    die; // always die after Location:
}
$page = $page ?: 'home'; // make it home if empty

if (preg_match('~^[a-z_-]+$~', $page) && file_exists("inc/$page.php")) {
    require "inc/$page.php";
    include "tpl/$page.tpl.php";
} else {
    http_response_code(404);
}
$page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (in_array($page, ['index.php', 'home'], true)) {
    header("Location: /", true, 301);
}
if (preg_match('~^[a-z_-]+$~', $page) && file_exists("inc/$page.php")) {
    require "inc/$page.php";
    include "tpl/$page.tpl.php";
} else {
    http_response_code(404);
}
$page = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$page = trim($page, "/");
if (in_array($page, ['index.php', 'home'], true)) {
    header("Location: /", true, 301);
    die; // always die after Location:
}
$page = $page ?: 'home'; // make it home if empty

if (preg_match('~^[a-z_-]+$~', $page) && file_exists("inc/$page.php")) {
    require "inc/$page.php";
    include "tpl/$page.tpl.php";
} else {
    http_response_code(404);
}
added 830 characters in body
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51
Loading
added 211 characters in body
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51
Loading
Source Link
Your Common Sense
  • 8.8k
  • 1
  • 21
  • 51
Loading