Forum Moderators: coopster

Message Too Old, No Replies

Setup for URL rewriting

Taking id=30 to id=the-page-title

         

xKillswitchx

1:42 am on Jan 19, 2009 (gmt 0)

10+ Year Member



I've got a custom CMS I've been working on for awhile and have friendly URL's implemented due to parsing the URL and gathering values from that.

I am working on a custom forum now that uses $_GET['cateogry'] and the same with the id of the forum. Since this was done different ( the CMS used $_GET['category'], and exploded everything after that to get values into an array ), how do I take id=30 and turn that into /the-page-title.html ? Do you have to overwrite the values for $_GET['id'] or how does this work exactly?

Thanks for any help on the matter, have very little experience with this topic.

techtheatre

5:36 am on Jan 19, 2009 (gmt 0)

10+ Year Member



I did not follow all that, but you can easily pass multiple variables to your main CMS script using mod_rewrite. You need to create a .htaccess file in the root of your web server (google ".htaccess" and also "mod_rewrite" for more info) and it might look something like this:

Options -MultiViews
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule /(.*)/(.*)/(.*)/ cmsscript.php?pf=$1&pn=$2&action=$3

In this case
[mysite.com...]
can be re-written as
[mysite.com...]

enigma1

1:38 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whatever function in your package creates the html link must take some arguments, one of them is say id=30. Therefore the function can execute a query in the database to retrieve the name of the category that has an id of 30.

No $_GET parameter is involved for the url encoding. Since the package is most likely unaware of the conversions, the decoder has to be implemented in the .htaccess with something similar to what posted above and another part inside the php script (cmsscript.php from above) that will re-populate the superglobals so everything will be transparent to the rest of the code.

jdMorgan

3:13 pm on Jan 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



xKillswitchx,

Also, if you are on Apache 2.x, look into using AcceptPathInfo to pass your 'SEO-friendly virtual URLs' to your script.

If you use this method, then you can replace the '$_GET explode' routine with one that takes the variables directly from the URL-path. Then look up the category or item using its friendly URL as the database lookup key, instead of its 'id' number.

---

A note to techtheatre:

Your code will execute up to hundreds of times faster with a few simple changes:


Options -MultiViews +FollowSymLinks +Indexes
RewriteEngine on
#
RewriteRule /([^/]+)/([^/]+)/([^/+])/$ cmsscript.php?pf=$1&pn=$2&action=$3 [L]

I assume that your URLs to be passed to your script always have three "directory levels" and end with a slash. If this is the case, this modified code will be functionally identical to your original, but much faster. If this is not the case, then you can still use the negative-match method demonstrated here to allow at least most of your URL-path-parts to be matched against the pattern in a single left-to-right pass, rather than the dozens to thousands of 'back-off-and-retry' match attempts required when using the multiple ".*" sub-patterns -- each of which initially matches "anything and everything."

You can interpret the new "([^/]+)" subpatterns in two equivalent ways, the second of which reveals why it's faster: It means "match one or more characters not a slash" and "match one or more characters until you find a slash, then stop." Compare this to "(.*)" which will initially match the entire requested URL-path, and then have to back off and re-try, one character at a time starting at the end of the requested URL-path, trying to leave just enough characters for the last, and then the next-to-last "(.*)" subpatterns to match. This is a *very* inefficient process when multiple "(.*)" subpatterns are used.

"RewriteBase /" is the default, and need not be specified unless there is a non-default RewriteBase preceding this one in the file. So I removed it.

(I don't want to hijack this thread. If further discussion is warranted, we can open a new thread. However, it is in the interest of the members here to stop the spread of inefficient code.)

Jim

coopster

11:06 pm on Jan 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Something else to add here ... the $_SERVER['PATH_INFO'] variable works great for this, BTW. Just be certain to check for it's existence first because if no path info follows the script name you will have no $_SERVER['PATH_INFO'] index set!