ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Setting up the 301 redirect with PHP in templates - Important for SEO / GOOGLE

March 16, 2012 4:29pm

Subscribe [2]
  • #1 / Mar 16, 2012 4:29pm

    Hi all,

    I’m trying to minimize some Crawl Errors and Duplicate Content issues in Google Webmaster Tools for article detail pages on my client’s site.

    What I’m trying to do is tell Google to only go to and index this url structure for the articles site_url/weblog_url/details_template/url_title/entry_id

    I’ve enabled PHP for the details template and included this at the top:

    {if segment_4==""}
    {exp:weblog:entries weblog="weblog" disable="categories|member_data|pagination|trackbacks" dynamic="on" limit="1" show_future_entries="yes"}
        <?
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: /{segment_1}/{segment_2}/{segment_3}/{entry_id}/" );
        die();
        ?>
    {/exp:weblog:entries}
    {/if}
    {if segment_5!=""}
    
        <?
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: /{segment_1}/{segment_2}/{segment_3}/{segment_4}" );
        die();
        ?>
        
    {/if}

    First one tells Google if no entry_id exist, insert it and redirect with the 301. Second one just tells it to get rid of any 5th url_segments and do the same. That one is really optional but I like it in there. This all works.

    If I try to add something like this for incorrect entry_id:

    {if "{entry_id}" != "{segment_4}" AND "{url_title}" == "{segment_3}"}
    {exp:weblog:entries weblog="weblog" disable="categories|member_data|pagination|trackbacks" dynamic="on" limit="1" show_future_entries="yes"}
        <?
        Header( "HTTP/1.1 301 Moved Permanently" );
        Header( "Location: /{segment_1}/{segment_2}/{segment_3}/{entry_id}/" );
        die();
        ?>
    {/exp:weblog:entries}
    {/if}

    It throws the browser for a loop and I get this error message:

    The page isn’t redirecting properly
    Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

    Is this possible?

    FYI, I’m using a simplified template for testing with no header embeds.

    -Martin

  • #2 / Mar 17, 2012 9:22pm

    glenndavisgroup

    436 posts

    Why not use htaccess for this?

    Mike

  • #3 / Mar 20, 2012 5:00am

    Really? You know of a way to validate url_title vs entry_id url segments using .htaccess? If yes, show me.

    Martin

  • #4 / Mar 23, 2012 1:55am

    Just as an FYI, I’ve already included the canonical_url tag for search engines, but wanted to ensure that any entries with the correct url_title but incorrect entry_id got redirected… it’s all based on the url_title and of course dynamic=“on”

    Since I do have the canonical_url should I just not worry about this?

    Martin

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases