Clean Hierachical URL's

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Clean Hierachical URL's

Post by Ted »

Shouldn't

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-d [NC]
look for existing directories first and then continue on if it's not found?
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Clean Hierachical URL's

Post by Elijah Lofgren »

Thanks to both Ryno and iNSiPiD.  ;D
Today I learned more about mod_rewrite (by actually READING the manual) and fixed a problem that I didn't know I had created.
Ryno wrote: Hey Elijah,

Just tried your .htaccess settings and they work great for me.

Only problem is when I try to access /admin or admin/ I get a 404 error?
Thanks, for pointing that out. I had actually not run into this problem because I was using Firefox's URL auto-complete. Thanks for finding it.  ;)
See below for fix.
iNSiPiD wrote: The rewrites above only account for existant files, not directories.

You need to create a rule for each directory you'd like access to. This also has the side affect of disallowing entry to folder views which, in most cases, is a good thing.

Just throw this into your htaccess file above the main rewrite rules.

RewriteRule ^/?admin/?$ /admin/index.php [R]
Thanks iNSiPiD for leading me to a solution. The above rule didn't work, but it I changed the [R] to a [L] it worked ([R,L] would also work, but we don't need to redirect the browser).

From the manual at: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
  • 'last|L' (last rule)
    Stop the rewriting process here and don't apply any more rewrite rules. This corresponds to the Perl last command or the break command in C. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL ('/') to a real one, e.g., '/e/www/'.
  • 'redirect|R  [=code]' (force redirect)
    Prefix Substitution with http://thishost[:thisport]/ (which makes the new URL a URI) to force a external redirection. If no code is given, a HTTP response of 302 (MOVED TEMPORARILY) will be returned. If you want to use other response codes in the range 300-400, simply specify the appropriate number or use one of the following symbolic names: temp (default), permanent, seeother. Use this for rules to canonicalize the URL and return it to the client - to translate ``/~'' into ``/u/'', or to always append a slash to /u/user, etc.
Here is the rule that worked to fix the /admin/ 404 error:

Code: Select all

# Make /admin/ work.
RewriteRule ^/?admin/?$   /admin/index.php [L]
And here is the fixed version of my .htaccess file:

Code: Select all

php_flag magic_quotes_gpc Off
php_flag register_globals Off
php_flag session.use_trans_sid Off

Options +FollowSymLinks
RewriteEngine on

# Make /admin/ work.
RewriteRule ^/?admin/?$   /admin/index.php [L]

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /parent/child/ 
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
Thanks again,

Elijah
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Clean Hierachical URL's

Post by Elijah Lofgren »

Ted wrote: Shouldn't

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-d [NC]
look for existing directories first and then continue on if it's not found?
I think it should from reading the manual, but I just tried it out and was unable to use it to prevent the 404 error for /admin/.  :-\
I guess I'll just have to use that extra line in my .htaccess file (see my last post above) unless someone comes up with a better solution.  ;)
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Clean Hierachical URL's

Post by Elijah Lofgren »

Ted wrote: Shouldn't

Code: Select all

RewriteCond %{REQUEST_FILENAME} !-d [NC]
look for existing directories first and then continue on if it's not found?
Thanks Ted!  :D

I just tried one last time by adding that line above another one (got idea from earlier post in this thread) and it works!  ;D
That special line for /admin/ is no longer needed.

The latest version of my .htaccess file:

Code: Select all

php_flag magic_quotes_gpc Off
php_flag register_globals Off
php_flag session.use_trans_sid Off

Options +FollowSymLinks
RewriteEngine on

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
Sorry about pasting my .htaccess file so many times in this thread.
I really should learn more .htacccess stuff before posting stuff that doesn't quite work over and over again.  :-[

Thanks again Ted!

Having developers that help out with things like this is what makes CMSMS rock so much!  ;)
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Clean Hierachical URL's

Post by Elijah Lofgren »

I just found out that not all servers support "php_flag" options in .htacces files.
Before I took out the php_flag lines I would get a "500 Internal Server Error" and would get this error in my error log:
.htaccess: Invalid command 'php_flag', perhaps mis-spelled or defined by a module not included in the server configuration
My latest .htaccess file is this:

Code: Select all

Options +FollowSymLinks
RewriteEngine on

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
Relevent options in config.php

Code: Select all

#Show mod_rewrite URLs in the menu?
$config['assume_mod_rewrite'] = true;

#Extension to use if you're using mod_rewrite for pretty URLs.
$config['page_extension'] = '/';

Hope this helps someone.  :)
Last edited by Anonymous on Fri May 05, 2006 12:57 am, edited 1 time in total.
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm
Location: North West England

Re: Clean Hierachical URL's

Post by Russ »

I'm no expert at this stuff, but I seem to remember that some search engines add a trailing slash to the path so instead of:
dir/subdir/page
you get
dir/subdir/page/
which could case problems. Could the $ not be /?$ or soemthing like that - I'm no expert on this and your code may already do this?

Russ
User avatar
Elijah Lofgren
Power Poster
Power Poster
Posts: 811
Joined: Mon Apr 24, 2006 1:01 am
Location: Deatsville, AL

Re: Clean Hierachical URL's

Post by Elijah Lofgren »

Russ wrote: I'm no expert at this stuff, but I seem to remember that some search engines add a trailing slash to the path so instead of:
dir/subdir/page
you get
dir/subdir/page/
which could case problems. Could the $ not be /?$ or soemthing like that - I'm no expert on this and your code may already do this?
The code above defaults to having pages have urls like dir/subdir/page/ and if someone visits  dir/subdir/page (without the slash) they will get a 301 redirect to the page with the slash  dir/subdir/page/ ;)
Note: I don't have time to take on any more projects. I'm quite busy. I may be too busy to reply to emails or messages. Thanks for your understanding. :)
Dj_Apx

Re: Clean Hierachical URL's

Post by Dj_Apx »

Hello, I was about to post a way to have this done in 0.12 savusavu without changing the code (just by adding 2 files), when i noticed that .13 already did it.

HOWEVER,

the aliases should NOT use underscores ( _ ) but either hyphens ( - ) or comas ( , ) if you want the urls to actually be search-engine friendly. This has been proved many times on the SEO forums (i'm a moderator at webrankinfo.com).

All the best,
Dj Apx
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Clean Hierachical URL's

Post by Ted »

Thanks for that heads up.  I'll make that change to the code.
Post Reply

Return to “Developers Discussion”