Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Author Topic: How mod_rewrite and get_id() work to create urls  (Read 2078 times)

nvision

  • Newbie
  • *
  • Karma: 1
  • Posts: 5
How mod_rewrite and get_id() work to create urls
« on: September 19, 2008, 06:09:35 pm »

Hello,

I want to fully understand the relationship of the Mod-Rewrite directives and function get_id(). I don't understand Apache directives all that well.

Here is what I think is going on, please correct me or fill in the blanks.

Code: [Select]
<IfModule mod_php4.c>
    php_value session.use_trans_sid 0
</IfModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
RewriteEngine On
#RewriteBase /sNews16

Basically, up to this point all request get redirected to the root/sNews16. Next is what I'm unclear about.   I don't understand whats going on in these rules

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z_]+)/([^/]+)/([^/]+)/ index.php?category=$1&title=$2&commentspage=$3 [L]

Code: [Select]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?category=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z_]+)/([^/]+) index.php?category=$1&title=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z_]+)/([^/]+)/([^/]+)/ index.php?category=$1&title=$2&commentspage=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9_-]+)/([0-9]+)/ index.php?category=$1&nbsp;articlespage=$2 [L]

I think that the parameters are being assign somehow here to category, title, commentspage, or articlespage.  But for what reason? its unclear to me.  In Function get_id() we are only reading "category" with $_GET['category']

FUNCTION get_id($parameter)...

I think what is happening here after exploding $_GET['category'] in to array $url
with recursive calls we return the value that matches the $parameter name.  This done by array position.

So that url[0] is the category name, url[1] is the article name, url[2] is the commentspage data if they exist.




Anyway, I know this was particularly long winded.

Thanks,
Logged

funlw65

  • Hero Member
  • *****
  • Karma: 96
  • Posts: 771
    • Country Lab
Re: How mod_rewrite and get_id() work to create urls
« Reply #1 on: September 19, 2008, 06:19:08 pm »

Logged

nvision

  • Newbie
  • *
  • Karma: 1
  • Posts: 5
Re: How mod_rewrite and get_id() work to create urls
« Reply #2 on: September 23, 2008, 04:31:46 pm »

Okay I checked out that post and also have done a bit of studing of mod_rewrite.  I am posting here for both confirmation and for anyone else that may be wondering how mod_rewrite and snews work together.  So please do correct me if I am wrong!

Here are two links I found useful.
http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/
http://www.workingwith.me.uk/articles/scripting/mod_rewrite


RewriteEngine On                                            # Turn it on!
RewriteBase /sNews16                                     # substitute your directory here
RewriteCond %{REQUEST_FILENAME} -f          # IF the file name exists then use it.  allows other files like  'readme.html'
RewriteRule ^(.+) $1 [L]                                    #  seems to pass the full query if present to that file and exit. [L] last rule skip the rest
 
RewriteCond %{REQUEST_FILENAME} !-d        # IF filename NOT a directory
RewriteRule ^(.+)/$ index.php?category=$1 [L]   # redirect to index.php and set the query value to 'category' 
                                                                      # This is read by sNews as $_GET['category'],
                                                                      # sNews FUNCTION get_id() explodes $_GET['category'] into an array of its parts using '/' as the delimiter


RewriteRule ^(.+)$ $1/ [L,R=301]                       # some magic here but essentially adds a slash to url if not present.; which is desirable



These trailing directives in the supplied htaccess I think, are not necessary and are superfluous -
These rules don't seem to do anything that I can tell. They initially confused me as they imply some purpose.


RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z_]+)/([^/]+) index.php?category=$1&title=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z_]+)/([^/]+)/([^/]+)/ index.php?category=$1&title=$2&commentspage=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9_-]+)/([0-9]+)/ index.php?category=$1&nbsp;articlespage=$2 [L]

Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: How mod_rewrite and get_id() work to create urls
« Reply #3 on: September 23, 2008, 04:40:58 pm »

Yes... it appears to be true... that the last 6 directive strings are redundant and not needed. This was discussed in this thread (Nov.07). I've since removed them from several of my sNews1.6 powered sites with no ill effect. I assume they were orphans left in there accidentally by the sNews Project's previous lead developers.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU