Friendly URLs

General project discussion. NOT for help questions.
Post Reply
iNSiPiD

Friendly URLs

Post by iNSiPiD »

I think this topic is important enough to warrant a new thread. Also, strictly speaking it can be accomplished without the CMS.

Further to my excited ramblings in 0.10beta3 I have good news.

I've managed after much re-writing (excuse the pun) to get a working version of my global rule working. It will rewrite any URL of the form yourdomain/alpha$/ with or without a trailing slash to its actual counterpart.

RewriteRule ^([A-Za-z]+)/?$ /index.php?page=$1 [R]

However it only works for single alpha strings and some of the pages I want to rewrite include the underscore character in them.

The pipe character is used to separate strings so I tried ([A-Za-z]+)|([A-Za-z]_[A-Za-z]) but to no avail.

I'm sure there must be a wild card expression that can account for all any and all characters. I thought it was (.*) but that doesn't work either. God on a raft, if only I could find some decent freaking documentation out there!

Unfortunately the above rule still can't differentiate between a directory and a file. So if you tried to view the directory contents for yourdomain/images/ it would rewrite to yourdomain/index.php?page=images. Not that that's a bad thing! In fact, security-wise it's quite nice.

Any advances on this are much appreciated.
iNSiPiD

Re: Friendly URLs

Post by iNSiPiD »

I DID IT!! I DID IT!! I'm SO excited. And I just can't hide it.

*************************************************************************
*                                                                      *
*  Rewrite all URL requests in the form of domain/$1/ to dmain/$1 and  *
*  translate to cmsMS URL form of domain/index.php?page=$1 while still  *
*  retaining the friendly URL in the browser's address bar.            *
*                                                                      *
*************************************************************************
RewriteEngine on

RewriteRule ^([a-zA-Z0-9]+[a-zA-Z0-9_]*[a-zA-Z0-9]*)/$ /$1 [R]
RewriteRule ^([a-zA-Z0-9]+[a-zA-Z0-9_]*[a-zA-Z0-9]*)$ /index.php?page=$1


Now if only one of you tricky dicky programmers can implement this into cmsMS so that it takes the Page Alias string and converts it automatically I'll totally pee my pants!!! :P
Jaani

Re: Friendly URLs

Post by Jaani »

Hi there,

I've been doing this since 0.8 using mod_rewrite.  It rewrites /index.php?page=alias to /alias, which I think is what you're doing.  I use a slightly different rewrite rule.  It seems to work for arbitrary levels of nesting (ie, /$1/$2/$3.../$n) - just modify the Alias accordingly.

Code: Select all

<IfModule mod_rewrite.c>

  # Enable URL rewriting
  
  RewriteEngine on
  RewriteBase /
  RewriteRule ^\.htaccess$ - [F]


  # Rewrite URLs of the form 'index.php?page=x&print=true':

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^print/(.*)$      index.php?print=true&page=$1 [L,QSA]


  # Rewrite URLs of the form 'index.php?page=x':

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$            index.php?page=$1 [L,QSA]


  # Rewrites /page.php as /page
  #RewriteRule ^/?(.*)\.php$ $1 [QSA]

  # Rewrites /page as index.php?page
  #RewriteRule ^([^/]*)$     index.php?page=$1 [L,QSA]

  # Disables admin rewriting
  #RewriteRule ^/?admin/?$   /admin/index.php [QSA]

</IfModule>
One thing I like to do with this is have fancy looking submit URLs for forms.  Eg, /home/contact-us, followed by /home/contact-us/submit for the processing page.  For the contact-us page, the alias is just 'contact-us'.  For the submit page (which has a processing blob [I'm not sure if this is the best way to do one-off PHP processing in CMSMS, but it seems to work]), the alias is 'contact-us/submit'.  All this is directly editable from within the Admin interface.

Edit: Check out http://slfr.jaani.net/ for an example of a test site I've deployed with CMSMS.

Just make sure you have absolute URLs to the stylesheet and images in your templates, or things will break when they get nested.

Regards,

Jaani.
Last edited by Jaani on Fri May 27, 2005 1:54 pm, edited 1 time in total.
iNSiPiD

Re: Friendly URLs

Post by iNSiPiD »

Hi Jaani,

Whoa! That's somewhat beyond my mod_rewrite level of skill. Very nice. I don't even know what half of it does. I tried to find an API but couldn't.

Where can I get a list of definitions for all the letters in braces? In particular what does [F] and [L, QSA] do? I assume !-f and !-d mean NOT file or directory.

I would like to somehow refine the solution so that absolute URLs weren't needed. They present too many problems for me when moving sites and playing with the directory structure (even though I shouldn't). For some reason the URL of the form domain/directory doesn't break the relatibe links. It's only whan you add the trailing slash.

So how do you get the multiple levels to happen? i.e. domain/contac/submit

I'm quite surprised there isn't more interest in this topic. I'd love to see it one day built into the CMS for Page Alias function.

NB: That's a very nice site and a very worthy cause. Also, I receievd an error on this page. http://slfr.jaani.net/register.html.
Jaani

Re: Friendly URLs

Post by Jaani »

Insipid,

Thanks, it works quite well for my purposes.  Regarding that error you pointed out, it's just a broken link that results in a 404.  For some reason, CMSMS doesn't display a nice error page but instead throws an error!  The same would occur if you try to access any page that does not exist.  For some time I've been searching for a way to prevent this, to no avail; it presents quite a bad image to the user, and doesn't give them anywhere to go from the error page.

Regarding the braces: I suggest you check out the Apache documentation - there are some great resources and tutorials on the Apache website.  Most of the tutorials on other enthusiast/programming sites are fairly mediocre, however.

See further:

http://httpd.apache.org/docs-2.0/misc/rewriteguide.html
http://httpd.apache.org/docs/mod/mod_rewrite.html

Multiple levels in the URL 'just works'.  The functionality is already built in to CMSMS and works fine using my .htaccess file as outlined above.

Also, absolute URLs aren't as much of a pain as you might think: they can just be -- they need not include the FQDN, so they're still quite portable.  The following are all valid URLs that will obviate any path problems caused by hierarchical nesting: "/index.php", "/images/top.gif", "/contact/submit", etc.

Adding the trailing slash makes Apache treat the page as occuring in a subfolder.  To my knowledge the only way to get around nesting problems like this is to use non-relative URLs.

I too would like to see further discussion on this topic.  The default '.shtml' extension is a little 1999 -- a customisable rewriting scheme would be a great feature, especially if it could be configured from within the administration interface.

Regards,

Jaani.
Last edited by Jaani on Mon May 30, 2005 5:45 am, edited 1 time in total.
jd

Re: Friendly URLs

Post by jd »

Hi,

I've the following error when I tried to use this .htaccess technique:

Code: Select all

Warning: Smarty error: unable to read resource: "db:contact" in /raid/www/localhost/root/lib/smarty/Smarty.class.php on line 1088
Any ideas why this might come out?
iNSiPiD

Re: Friendly URLs

Post by iNSiPiD »

Can't read the error jd. Can u just paste it in normally?
Post Reply

Return to “General Discussion”