.htaccess help with renaming files and folders


#1

Hi everyone

I’m just experimenting with some code at the moment which will automatically scan the root directory, and then build the main navigation on the fly from retrieved file and folder names.

But an issue I have is the ordering of the navigation items, at the moment items are ordered left to right alphabetically based on the names of files and folders in the root directory.

So the only method for the client in Surreal CMS is to make a certain files (nav) or folders (nav with subnavs) appear in a specific order in the navigation is the use a number as part of the file/folder name eg. 1-home.php,2-about.php, 3-projects.

HTML Code

My code will strip out the 1-, 2-, 3- from the file and folder names in the rendered HTML, but the actual files and folders in the root directory will still contain them to maintain the order.

This HTML code is generated automatically by the script eg file names and HTML tags

<ul>
  <li><a href="home.php">Home</a></li>
  <li><a href="about.php">About</a></li>
  <li>
      <a href="projects/">Projects</a>
        <ul>
          <li><a href="projects/subpage.php">Subpage</a></li>
      </ul>
  </li>
</ul>

I have used .htaccess to rewrite the url paths (in the browser) without the 1-, 2-, 3- etc

from

mysite.com/1-home.php
mysite.com/2-about.php
mysite.com/3-projects/
mysite.com/3-projects/subpage.php

to

mysite.com/home.php
mysite.com/about.php
mysite.com/projects/
mysite.com/projects/subpage.php

Rewrite example for mysite.com/3-projects/subpage.php to mysite.com/projects/subpage.php

# 3 folder/projects/subpage
RewriteCond %{REQUEST_URI} /projects/subpage.php$
RewriteRule .* 3-projects/subpage.php [L]

The real content is still from 1-home.php, 2-about.php and 3-projects/subpage.php in the root.

Although this can be done if you know before hand the names of the files, folders and the order which they have been setup on the server so you can then implement the rewrite conditions to match it.

The issue is when you don’t know what the client will be naming their files, folders and the order before hand.

I was just wondering if anyone else had a better idea how to do this, if it is even possible?

Thanks


#2

If you’re hard coding numbers into filenames, why not just hard code the pages into your nav script instead?

Your nav may look like this, with the most important pages first:

  1. Homepage
  2. About
  3. Projects
  4. (additional links here could be hard coded or made variable depending on your needs)

A subnav of Projects could be generated by iterating the projects directory. The same technique could be used for any other subnavs you need.

If you can live with a more rigid top-level, I think you’ll be able to simplify things quite a bit and get rid of those .htaccess rewrites :slight_smile:


#3

I could hard code the filenames, but I am trying to write a script to automatically write the filenames based on the root directory files, so it can be independent of me and the client. This will allow the client to create a new page or folder in Surreal CMS and have the script automatically update the navigation to reflect the new pages or subpages.

So hard coding the files would not work as I would have to intervene and update the code, which is basically what I do now with other sites when the client needs a new page added to nav.

Thanks for your reply Cory.


#4

I was thinking the top-level would have specific items that wouldn’t change (e.g. Homepage, About, etc.) followed by the custom items with subnavs that will change. You’d still have to rely on convention (or alphabetical order) for custom items, though.


#5

I see what you mean now Cory :+1:.

So hard code all the top level pages that should not need to change on the site, and then allow the custom items/pages be added by the client after those items.

I think this is a good compromise on the custom ordering of the nav items and still allowing the client to automatically add to the navigation by creating new pages on the server.

Thanks for the help on this Cory, I appreciate having another perspective on this.