Create Blog Post Links Similar to a Folder Structure

Create blog post links similar to a folder structure

You will need a few things:

  1. Setup an .htaccess to redirect all request to your main file which will handle all that, something like:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    The above will redirect all request of non-existent files and folder to your index.php

  2. Now you want to handle the URL Path so you can use the PHP variable $_SERVER['REQUEST_URI'] as you have mentioned.

  3. From there is pretty much parse the result of it to extract the information you want, you could use one of the functions parse_url or pathinfo or explode, to do so.

Using parse_url which is probably the most indicated way of doing this:

$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "https" : "http";
$url = $s . '://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
var_dump(parse_url($url));

Output:

["scheme"] => string(4) "http" 
["host"] => string(10) "domain.com"
["path"] => string(36) "/health/2013/08/25/some-random-title"
["query"] => string(17) "with=query-string"

So parse_url can easily break down the current URL as you can see.

For example using pathinfo:

$path_parts = pathinfo($_SERVER['REQUEST_URI']);

$path_parts['dirname'] would return /health/2013/08/25/

$path_parts['basename'] would return some-random-title and if it had an extension it would return some-random-title.html

$path_parts['extension'] would return empty and if it had an extension it would return .html

$path_parts['filename'] would return some-random-title and if it had an extension it would return some-random-title.html

Using explode something like this:

$parts = explode('/', $path);
foreach ($parts as $part)
echo $part, "\n";

Output:

health
2013
08
25
some-random-title.php

Of course these are just examples of how you could read it.

You could also use .htaccess to make specific rules instead of handling everything from one file, for example:

RewriteRule ^([^/]+)/([0-9]+)/([0-9]+)/([0-9]+)/([^/]+)/?$ blog.php?category=$1&date=$2-$3-$4&title=$5 [L]

Basically the above would break down the URL path and internally redirect it to your file blog.php with the proper parameters, so using your URL sample it would redirect to:

http://www.mysite.com/blog.php?category=health&date=2013-08-25&title=some-random-title

However on the client browser the URL would remain the same:

http://www.mysite.com/health/2013/08/25/some-random-title

There are also other functions that might come handy into this for example parse_url, pathinfo like I have mentioned early, server variables, etc...

changing the URL structure of lektor for blog posts to be in the parent directory

This is possible. If you started with the default setup from lektor quickstart:

  • In models/blog-post.ini, remove hidden = yes
  • In models/blog.ini, under the pagination section, add items = site.query('/').filter(F._model == 'blog-post')

The first step lets you create blog posts under root in the admin.
The second step tells the page blog to look for its pagination items under /, and only include the ones of type blog-post. This will exclude e.g. About, Blog, etc.

You will now have your site's home page at /, a blog page with list of all posts at /blog, and each post will be located at example.com/my-blogpost-about-food/.

To create a new blog post in the admin, go to the root admin page (not the /blog admin page), click the +, and select Blog Post for your new page type. It will show up in the list on /blog, and live directly under /.

Jekyll Folder Structure. Having folders/lists on a page which each have specific posts

This should work by adding the path to your for loop. For example, let's assume you have a directory in your _blog directory called "orange." So your file path would be _blog/orange. To loop through only the files in the orange directory you could do this:

{% for post in site.blog.orange %}
{{ post.do-stuff }}
{% endfor %}

On a side note, Jekyll is going to look for blog posts in the _posts directory. I assume this _blog directory is a collection, but creating a "blog" collection will make things confusing if anyone else ever needs to touch this codebase.

Print Directory & File Structure with icons for representation in Markdown

If you are concerned about Unicode characters you can use ASCII to build the structures, so your example structure becomes

.
+-- _config.yml
+-- _drafts
| +-- begin-with-the-crazy-ideas.textile
| +-- on-simplicity-in-technology.markdown
+-- _includes
| +-- footer.html
| +-- header.html
+-- _layouts
| +-- default.html
| +-- post.html
+-- _posts
| +-- 2007-10-29-why-every-programmer-should-play-nethack.textile
| +-- 2009-04-26-barcamp-boston-4-roundup.textile
+-- _data
| +-- members.yml
+-- _site
+-- index.html

Which is similar to the format tree uses if you select ANSI output.

File structure of a website URL path with multiple slashes to html file

It honestly depends on the file structure of the website! Usually pages are found in a logical order like you mentioned, from what I've seen so far underneath folders as the path. However this may change when working with things such as REACT only applications where it's carried out through routing of a single-page application instead of following a linear tree-like structure. Not sure if I've explained this great but I hope it helps! :)

Correcting HTML sub folder links to main files

use this "/" to refer to the root folder

Like this

<nav><ul style="list-style-type:none">
<li><a href="/Info.html">Info</a></li>
</nav>


Related Topics



Leave a reply



Submit