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

Paul on Sep 29 2010

Shorten your WebsiteBaker URLs by removing /pages/

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.

See also:

WebsiteBaker: Removing /pages/ from the URL WebsiteBaker: Removing /pages/ from the URL
Shorten your WebsiteBaker URLs by removing /pages/

Newer topics:

How to fix Firefox search to use the local Google search How to fix Firefox search to use the local Google search
Does your Google search box send you to google.com instead of the site for your country? Here's the fix.

Web Hosting: Beware of Fake Review Sites Web Hosting: Beware of Fake Review Sites

Create Your Own Layer for Google Maps on Android Create Your Own Layer for Google Maps on Android
Here's how to use POI data as a Maps or Nav layer.


Comments are moderated so there may be a delay before your comment appears. Thanks for your patience.

Nov 02 2011

Paul

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).

Nov 02 2011

KEnik

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

Jul 13 2011

Paul

Hi Kenik,

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

Jul 13 2011

Kenik

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.