JavaEar 专注于收集分享传播有价值的技术资料

best way to handle page title and SEO items with many pages?

My site is slowly getting bigger and im realizing i dont want to keep going back through at the end and editing the meta tags and titles etc.

My idea was to have a php page included in the head of every page.

I would then have multiple if statements which all did a preg_match to get the page url and then it would echo out the appropriate information.

if (preg_match('@page1?@i',$_SERVER['REQUEST_URI'])) {
    echo <title> Title </title>

and so forth.

Is this a reliable way of doing it? So that everything is in one file an 'easily' editable?


  1. Depends what you class as big. If your site has only 20 or so pages then it's fine, if you have more, or they change often, then its less good, and will soon get messy. Also it makes it very easy to accidentally add syntax errors, or break your site when editing live code.

    One possibility is to store the meta data for each URL in a database. Then instead of a range of if statements you can just look up the metadata for that URL with SQL.

    However, even easier is to use a pre built framework or cms (content management system). These have a learning curve, although many can be picked up quickly. I would recomend looking into Wordpress; it's open source, has a simple back end, good documentation and is reletavly easy to theme. Alternatively, you could use drupal, joomla, or build it yourself ontop of a framework such as cakePHP or Codeignter.

    To make your decision you should consider how maintainable the solution will be in the future, and how much time you are willing to invest developing the site. Each solution has it's problems, and some will be better suited to different scenarios.

  2. 参考答案2
  3. If your looking for a simple preg_match statement to determine the page from your URL then you could get away with something like:

    $uri = $_SERVER['REQUEST_URI'];
    //$uri = '/page43/';
    if (preg_match("/page([0-9]+)/i",$uri,$matches)){
        echo '<title> Title - Page '.$matches[1].'</title>';
  4. 参考答案3
  5. I think that you should put that code in one file and name it something like title.php and save in your root folder then use <?php iclude_once('title.php') ?> add the begining of each webpage.