WebsiteBaker: Removing /pages/ from the URL - The Code

Published: September 12, 2010

If you haven’t already read about my methods for removing /pages/ from a WB(WebsiteBaker) site URL, check out the previous article: WebsiteBaker - Removing /pages/ from the URL.

Before starting be sure to take a backup, just in case.

WB Code Changes

  1. Define PAGES_URL in /framework/class.frontend.php (seems to be the right place). In future it can/will be handled the same as PAGES_DIRECTORY. Near the top put:

    define('PAGES_URL','');
    
  2. Replace $_SERVER[‘PHP_SELF’] with $_SERVER[‘REQUEST_URI’]

    modules/mpform/paintform.php - line 228
    modules/news/view.php - line 197
    modules/form/view.php - lines 139,384,389,393

  3. Correctly use $wb->page_link($link)

    /modules/form/view.php - line 469
    /modules/news/rss.php - lines 95,96
    /modules/mpform/evalform.php - line 589

    Replace

    WB_URL.PAGES_DIRECTORY.{some_variable}.PAGE_EXTENSION
    

    with

    $wb->page_link({some_variable})
    
  4. Other

    /modules/topics/module_settings.php - line 87

    change to:

    $topics_virtual_directory = PAGES_URL.'/topics/';
    

    /framework/class.wb.php - line 161
    /index.php - line 88

    change PAGES_DIRECTORY to PAGES_URL

Apache Rewrites (.htaccess)

# --- Removal of /pages/ in URL --- #
# 1\. Point root to right file
RewriteRule ^$ /pages/home.php [L]
# 2\. /filename.php => /pages/filename.php - but only non-existing files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /pages/$1 [L]
# 3\. /*/filename.php => /pages/*/filename.php - but only non-existing files.
# Extra "cond" added to prevent /pages/pages/pages/pages (could also be done in RewriteRule)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !/pages/
RewriteRule ^([^/]+)/([^/]+)$ /pages/$1/$2 [L]
# 4\. /*/*/filename.php => /pages/*/*/filename.php - but only non-existing files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /pages/$1/$2/$3 [L]
# 5\. /*/*/*/filename.php => /pages/*/*/*/filename.php - but only non-existing files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /pages/$1/$2/$3/$4 [L]

# Redirect index page for folders under /pages/ (not /*/*.php,
 and only non-existing directories.)
# 1\. Redirect /*/ => /*.php   
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1.php [R=301,L]
# 2\. Redirect /*/*/ => /*/*.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [R=301,L]
# 3\. Redirect /*/*/*/ => /*/*/*.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /$1/$2/$3.php [R=301,L]
# 4\. Redirect /*/*/*/*/ => /*/*/*/*.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /$1/$2/$3/$4.php [R=301,L]

I can’t guarantee that everything here is perfect, so be sure to test it yourself before deploying live. If you spot any mistakes, or situations that aren’t covered, please let me know and I’ll correct the code.

Comments:

Thank you for this article!
I found litle wrong :

5. /*/*/*/filename.php => /pages/*/*/*/filename.php - but only non-existing files.  
RewriteCond %{REQUEST_FILENAME}  
!-f RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /pages/$1/$2/$3/$4 [L]  

Had bad line ending

\\\"!-f RewriteRule\\\"  

I add for this editing /admin/pages/add.php to make flat url. Url containts subdirs becouse on creating are cretaed. And simple added to admin template one input link.
I anyone can i will write an article about this.. solution.

Posted by Kenik on 13 Jul 2011


Hi Kenik,

Thanks for spotting the error. I accidently entered a line break before the !-f. I’ve corrected the code above.

Posted by Paul on 13 Jul 2011


delete PAGES_DIRECTORY form
ROOT/index.php line:88 header(‘Location: ‘.WB_URL.PAGES_DIRECTORY.$target_page_link.PAGE_EXTENSION.($anchor?‘#’.$anchor:“));

Posted by Kenik on 2 Nov 2011


Thanks Kenik, I’ve updated above. Yes, that line covers redirecting for menu links. Rather than removing it, using PAGES_URL would be better. Ideally the surrounding code should be replace with something involving $wb->page_link($target_page_id).

Posted by Paul on 2 Nov 2011


This is realy helpful information and THX for it!
I´m not a php programmer so I only can try it step by step as you worte it guys. But the last comments make me a litte confuse. The ideal way for this would be, a summary with all knowledge in one manual. I know this is some work and takes time, but when you go your way so far then the last mile maybe is the fastest one.

Other question: Will it be the same for lepton?

Posted by Werner on 19 Aug 2012


@Werner, I’m glad it’s helped you.

When you mention that the last comments are confusing, which exactly do you mean, the Apache rewrite code? Also, have you already looked at the previous post that explains this post?

As far as I know this should work for Lepton, but I’d always recommend backups and testing before going live.

Posted by paul on 26 Aug 2012

That’s brilliant!
As you say having the /pages within the URL doesn’t do any damage SEO wise, it’s just one of those little things which if removed makes the URL nice and tidy and simple.
To take it one step further, how could I remove the “.php” extension form the end also? So you end up with http:/www.url.com/level1/level2

Posted by Glen on 16 Feb 2014


 
@Glen, glad it helped you.

You could use a rule like this remove the php extension:

RewriteRule ^(.*)\.php$ $1 [NC]  

Or one like this to add the php extension to all URLs that contain no period/fullstop.

RewriteRule ^([^\.]+)$ $1.php [R=301,L,NC]  

I’d recommend lots of testing. ;)
 

Posted by paul on 16 Feb 2014