0

I've got a blog the url structure like this and strict urls's turned on.

www.site.com/blog This lists all the blog entries

www.site.com/blog/post/url_title This is the single entry page displaying the entry

Now, if www.site.com/blog/foo they'll still get a full list of blog entries. Would it be a good practice to add this at the top of /blog to prevent it happening?

{if segment_2 AND segment_3 == ''} {redirect="404"} {/if}

1

Personally I'd look to handle this at the server level in my .htaccess file so it keeps the template files a little cleaner and doesn't fire up EE's template parser when it's not needed.

Something like this should forward them on to the requested page but with the correct directory inserted into the URL:

RedirectMatch 301 /blog/(.*) http://domain.com/blog/post/$1

It's good for SEO purposes as it's set as a permanent redirect and it means they'll actually be forwarded on to the correct URL rather than meet with a 404.

You could then just maybe keep the {if no_results}{redirect="404"}{/if} in the template to catch anything which doesn't actually exist.

0

Also check the "require_entry" parameter (http://ellislab.com/expressionengine/user-guide/modules/channel/channel_entries.html#require-entry).

In one of my single entry blog templates I have a configuration like this:

{exp:channel:entries channel="blog" oderby="date" sort="desc" limit="1" require_entry="yes"}
{if no_results}{redirect="404"}{/if}
{if segment_4 != ""}{redirect="404"}{/if}
0

If there are only going to be 2 pages in your example, you could something like:

{if segment_1 == "blog"}
     {if segment_2 != ""}
          {if segment_2 != "post"}{redirect="404"}{/if}
          {if segment_3 == ""}{redirect="404"}{/if}
     {/if}
{/if}

(edited: removed incorrect parsing comment, see Ian's comment)

1
  • Just to point out that the template parsing still kicks in but the simple conditionals just gets processed at an earlier stage. It still fires up the template parser.
    – Ian
    May 20 '13 at 11:24

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.