include once - how to deal with include_once php urls?

373

I'm building a static site and trying to get a bit modular on the code. Using include_once solves my problem, but the new created section.php file is seen as another URL on the server and IMHO creates a SEO problem - duplicate content. Thin theory, but still. Is there any solution to use include_once and mark those included files as non-existent for crawlers?

Just a code example, to better define what the problem is.

index.php looks like this:

<div id="wrapper">
    <?php include_once ('header.php'); ?>
    <div id="content">
        <h1>Title</h1>
        <p>Page content</p>
    </div>
</div>

header.php looks like this:

<div id="header">
    <ul class="menu">
        <li>
            <a href="/">Home</a>
        </li>
        <li>
            <a href="#">About</a>
        </li>
        <li>
            <a href="#">Contact</a>
        </li>
    </ul>
</div>

So generated code will render like this:

<div id="wrapper">
    <div id="header">
        <ul class="menu">
            <li>
                <a href="/">Home</a>
            </li>
            <li>
                <a href="#">About</a>
            </li>
            <li>
                <a href="#">Contact</a>
            </li>
        </ul>
    </div>
    <div id="content">
        <h1>Title</h1>
        <p>Page content</p>
    </div>
</div>

header.php is another file on the server and could be indexed by search engine, because it has its own URL. Question might be dumb and may have the simplest answer, I just don't know if I should use redirects or some other tweaks.

Thanks!

871

Answer

Solution:

You can do it by multiple ways.

  1. Add a Robots.txt file
  2. In your included file add a key variable for authentication.

1- Robots.txt:
Save this as Robots.txt in your website's root directory.

User-agent: *
Disallow: /elements

Save your files in elements folder. Whatever will be in elements folder, Google and other search engines's Crawler will not crawl it. This will never list in search results.

2- Use Key Authentication index.php will looks like this:

>

<div id="wrapper">
     <?php $key = 'allow_this'; ?>
    <?php include_once ('header.php'); ?>
     <div id="content">
        <h1>Title</h1>
         <p>Page content</p>
     </div> </div>

header.php looks like this:

<?php if($key=='allow_this'){ ?>
<div id="header">
    <ul class="menu">
        <li>
            <a href="/">Home</a>
        </li>
        <li>
            <a href="#">About</a>
        </li>
        <li>
            <a href="#">Contact</a>
        </li>
    </ul>
</div>
<?php } ?>

People are also looking for solutions to the problem: php - Why Use unset() To Clear Array If I Can Re-Declare Using Same Variable Name?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.