Pretty URLs and ".../" == ".../index.html" [SOLVED]

Help with getting the CMS CORE package up and running. This does not include 3rd party modules, PHP scripts, anything downloaded via module manager or from any external source.
Locked
dtgriscom
Forum Members
Forum Members
Posts: 15
Joined: Fri Mar 04, 2011 8:59 pm

Pretty URLs and ".../" == ".../index.html" [SOLVED]

Post by dtgriscom »

I'm porting an existing site to CMSMS 1.9.3, and I'm using pretty URLs to maintain the existing (hierarchical) page organization. It's gone well (great tool!), but I have one issue: URLs that end with "/" aren't treated as the same as the same URLs with "index.html" appended.

This is how Apache treated my old website, and I'd like to continue it so that people's bookmarks continue working. But the default page appears at mySite.com/, but not mySite.com/index.html. And, other index pages I've created appear at mySite.com/myCategory/index.html, but not mySite.com/myCategory/.

Yes, I could create a redirect page for every directory, but that's painful and error-prone.

Any suggestions?

Thanks,
Dan
Last edited by dtgriscom on Tue Mar 08, 2011 4:16 pm, edited 1 time in total.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: Pretty URLs and ".../" being equivalent to ".../index.ht

Post by Wishbone »

Apache supports priority of index.* files... When my site is "Coming Soon" I set index.html to take top priority (with the coming soon message).. When my site is live, I set index.php to take top priority. I don't know how to set it manually, but there is the option in my hosting control panel for this.

You can do some mod_rewrite magic in your .htaccess file to remove all index.html references from the requesting URL. I don't know how to do it off the top of my head.. I have to look it up each time.
dtgriscom
Forum Members
Forum Members
Posts: 15
Joined: Fri Mar 04, 2011 8:59 pm

Re: Pretty URLs and ".../" being equivalent to ".../index.ht

Post by dtgriscom »

Actually, I need the opposite: URLs that end in "/" should have "index.html" appended. I'm sure it could be done with mod_rewrite, although that's one learning curve I have yet to climb. Unfortunately, this would mean that the default page (which always lives at "mysite.com/") would never be visited, which is workable albeit grody.

(Switching index.html and index.php priorities wouldn't work, since for CMSMS to work it all has to be routed through index.php.)

Given how common this feature is on Apache-only websites, it would be great if there were a CMSMS option that did this for us. Any ideas?
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am
Location: Arizona

Re: Pretty URLs and ".../" being equivalent to ".../index.ht

Post by Dr.CSS »

You can do mod_rewrite to add .html to the end/extension of all pages but unlike flat file sites you can't have mysite.com/some category/index.html ans mysite.com/another category/index.html as this system doesn't use folders for sections/categories like a flat file system...

You will have to redirect all incoming URLs to the new pages in a .htaccess file...
dtgriscom
Forum Members
Forum Members
Posts: 15
Joined: Fri Mar 04, 2011 8:59 pm

Re: Pretty URLs and ".../" being equivalent to ".../index.ht

Post by dtgriscom »

Well, I finally fixed it all with mod_rewrite. Here's my new .htaccess file:

Code: Select all

Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteBase /

# Rewrite top level "index.html" page and empty query
# to the default page for the website
RewriteRule ^index.html$  index.php?page=   [QSA,L]
RewriteRule ^$ index.php?page=	 [QSA,L]

# URLs ending in ".html" will be mapped to index.php with argument
# but only if the requested URL is not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L]

# If request ends in slash, assume it's	a directory and	get its	index
RewriteRule ^(.+)/$ index.php?page=$1/index   [QSA,L]

# If we haven't remapped anything, and the URL isn't a file,
# try assuming it's a CMS directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1/index [QSA,L]

# Anything else will fall through
Took a while to build (using successive approximation programming), but it seems robust now.


Thanks,
Dan
dtgriscom
Forum Members
Forum Members
Posts: 15
Joined: Fri Mar 04, 2011 8:59 pm

Re: Pretty URLs and ".../" being equivalent to ".../index.ht

Post by dtgriscom »

A few more tweaks and improved comments:

Code: Select all

Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteBase /

# Rewrite top level "index.html" page and empty query
# to the default CMS page
RewriteRule ^index.html$  index.php?page=   [QSA,L]
RewriteRule ^$ index.php?page=   [QSA,L]

# Allow access to admin area via "admin"
RewriteRule ^admin/?$ admin/index.php

# URLs ending in ".html" will be mapped to index.php with argument
# but only if the requested URL is not a real file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+).html$ index.php?page=$1 [QSA,L]

# If request ends in slash, assume it's a CMS directory and get its index
RewriteRule ^(.+)/$ index.php?page=$1/index   [QSA,L]

# If we haven't remapped anything, and the URL isn't a real file,
# try assuming it's a CMS directory. This will also ensure that
# any bad URL go through the CMS and display the CMS error page.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1/index [QSA,L]

# References to files (e.g. images) will fall through.

dtgriscom
Forum Members
Forum Members
Posts: 15
Joined: Fri Mar 04, 2011 8:59 pm

Re: Pretty URLs and ".../" == ".../index.html" [SOLVED]

Post by dtgriscom »

Well, I found another bug in my redirect code, so here's my latest shot:

Code: Select all

Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteBase /

# Rewrite top level "index.html" page and empty query
# to the default CMS page
RewriteRule ^index\.html$  index.php?page=          [QSA,L]
RewriteRule ^$             index.php?page=          [QSA,L]

# Allow access to admin area via "admin"
RewriteRule ^admin/?$      admin/index.php          [L]

# URLs ending in ".html" will be mapped to index.php with argument
# but only if the requested URL is not a real file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$   index.php?page=$1        [QSA,L]

# If request ends in slash, assume it's a CMS directory and get its index
RewriteRule ^(.+)/$        index.php?page=$1/index  [QSA,L]

# If we haven't remapped anything, and the URL isn't a real file,
# add a slash at the end and try again. This will also ensure that
# any bad URL go through the CMS and display the CMS error page.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$         $1/                      [R]

# References to files (e.g. images) will fall through.
(Boy, would it be nice if CMSMS handled this internally...)
Locked

Return to “[locked] Installation, Setup and Upgrade”