Auto Friendly URLs

9 posts by 2 authors in: Forums > CMS Builder
Last Post: July 3, 2012   (RSS)

By rez - June 26, 2012 - edited: June 26, 2012

I see several posts on this topic but i dont understand any of it.

I'm making a site of for a franchise. Each time a new record / location is created in CMSB, the business is added to a directory named locations.php. Simple details system. Click a location and it opens the details on location.php (singular) Something like:
website.com/location.php?bridgewater-5

The client wants each location to a friendly address for their page and business cards. Something like:
website.com/location/bridgewater

So no .php, no question marks and no record number.

I found this link with a final solution by dave:
http://www.interactivetools.com/iforum/Products_C2/CMS_Builder_F35/gforum.cgi?post=80752;search_string=friendly%20url;t=search_engine#80752

I don't really understand it yet so is this still the best way with CMSB? Will the friendly address actually appear in the address bar?

Thank you.

Re: [rez] Auto Friendly URLs

By Jason - June 26, 2012

Hi,

Dave's post will do most of what you want. It will, however, still have the record number at the end of the url. To get rid of that, you can simply manually output your urls like this:

EXAMPLE:
website.com/location/<?php echo $location['name'];?>

You will need to make sure you put this information into a variable in your .htaccess:

EXAMPLE:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^location/(.*?)$ location.php?name=$1 [L]


The relying completely on a name is normally not recommended. For example, if you had 2 locations with the same name, this would cause a problem. If the name of the location ever changed, the url printed in other locations (ie business cards) would no longer work.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Auto Friendly URLs

By rez - June 28, 2012 - edited: June 28, 2012

locations.php is my list page
location.php is my details page
page_name is what I am using for the "name" variable


.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^location/(.*?)$ location.php?page_name=$1 [L]


locations.php list
list($locationsRecords, $locationsMetaData) = getRecords(array(
'tableName' => 'locations',
'loadUploads' => true,
'allowSearch' => false,
'useSeoUrls' => true,
));


This appears to give me the seo friendly links with the record number still on the end.

Navigating to the home page or aboutus works as expected.

clicking locations takes me to the right list page:
website.com/locations.php

selecting a location still has ".php in the address:
website.com/location.php/bridgewater-5/

and from that detail page, if i click "about us" again, it tries to load:
website.com/location.php/bridgewater-5/aboutus.php

in fact from there on, any thing i click keeps the location in the address.



Is one of the problems the fact that I have the .htaccess in the root? At one point I had a location page loading without css. I dont want to have to have full links for everything. Can this be done somehow for just locations in a folder or something?

I also didn't understand what you meant on getting rid of the record number. When "page_name is "bridgewater", I can get either of these links to work:
website.com/location/bridgewater/
website.com/location.php/bridgewater/



thanks for more help on this, I'm still trying to fix it. :/

Re: [rez] Auto Friendly URLs

By Jason - July 2, 2012

Hi,

You are not going to be able to use the auto generated "_link" pseudo field to do this. What you would need to do is to manually output your links. Something like this:

EXAMPLE
<a href = "/location/<?php echo $record['name'];?>">Click here</a>

This should give you /location/bridgewater

One of the problems that comes from this, is linking to other pages, as you discovered. you can try putting a slash before you links:

ie

<a href = "/aboutus.php">About us</a>

or, you can output an absolute path:

ie

<a href = "http://<?php echo $_SERVER['HTTP_HOST'];?>/aboutus.php">About us</a>

Hope this helps get you started
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Auto Friendly URLs

By rez - July 2, 2012

and thats site wide? i cant contain this to only affect one folder (.htaccess in that folder or something)?

Re: [Jason] Auto Friendly URLs

By rez - July 2, 2012

website.com/location.php/watkinsville-2/
takes me to the page

website.com/location/watkinsville-2/
does not work (got rid of the .php)

/location.php/watkinsville
does not work (without the record #)

website.com/location/watkinsville
is what I want but also doesnt work (no .php or record #)

does this look right?

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^location/(.*?)$ location.php?page_name=$1 [L]

Re: [rez] Auto Friendly URLs

By Jason - July 2, 2012

Hi,

I don't see any immediate problems. My guess as to why it only works with a number is that you're using something like this:

whereRecordNumberInUrl(1)

in your where clause.

.htaccess rewrites are notoriously picky and get complicated pretty quickly. If you need us to take a look at this, we can go through consulting and see what options we have.

If you're interested in this, please send an email to
consulting@interactivetools.com

Hope this helps,
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Auto Friendly URLs

By rez - July 3, 2012

sent mail to consulting