Whats the best way to setup a url path so that I can use certain segments of the url as parameters (similar to $_GET but nicer looking).
For example: mysite.org/chapters/uncc or mysite.org/blog/some-article
Where "chapters" and "blog" are actual resources and "uncc" and "some-article" are parameters.
So, for example when someone goes to mysite.org/blog/some-article - it would go to the /blog resource and then I would use "some-article" as a parameter which I would use to query an Expression Engine database for content.
Ideally, for chapters, I would like to remove "chapters" from the url and use mysite.org/uncc, mysite.org/appstate, etc.
The reason I am using a second CMS for blogs / chapters is because we will have a large number of blogs / chapters and I don’t want the resource tree getting bogged down and making the whole manager run slow.
Any thoughts?
God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
Quote from: lucas at May 21, 2011, 12:50 PM
Try out the following amendment to your existing .htaccess file:
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(\w+)/?$ blog?param=$1 [QSA]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Great idea! I’m getting a server error right now when I add that line, but I’m working on tweaking it to make it work.
God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
Ok, this works:
# Send all blog pages requests to /blog
RewriteRule ^blog/(.*)$ index.php?q=blog¶m=$1 [L,QSA]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
and this works:
# Send all blog pages requests to /blog
RewriteRule ^blog/(.*)$ /blogs¶m=$1 [L,QSA]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
but this doesn’t work:
# Send all blog pages requests to /blog
RewriteRule ^blog/(.*)$ /blog¶m=$1 [L,QSA]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
It seems like rewriting blog to blog?whatever doesn’t work but blog to somethingbesidesblog?whatever does work.
God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
I’m currently working on a slightly different approach that allows me a bit more flexibility (still a work in progress). Additionally, my production server and local server required different .htaccess code to run correctly using the previous approach (sure I could have worked this out eventually but ...). Here’s my current approach.
1. Hook into onHandleRequest event (the entire $_REQUEST array is sanitized just before this event fires).
2. Some variation of the code below - eventually I will have an array of rewrite paths for any item that could potentially have alot of records (events, news, etc). Plugin code:
<?php
$rAlias = $modx->getOption('request_param_alias', null, 'q');
$urlPath = $_REQUEST[$rAlias];
$urlSegments = explode('/', $urlPath);
if ($urlSegments[0] == 'blog' && $urlSegments[1] = 'post') {
$_REQUEST[$rAlias] = 'blog/post';
$_REQUEST['qstring'] = str_replace('blog/post', '', $urlPath);
}
As you mentioned above, I could remove the ’qstring’ variable and parse the string once it gets to the resource, but for now I’m leaning towards keeping everything in one place.
God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
Here is the current iteration of my plugin - seems to be working and is easily configurable.
<?php
$rAlias = $modx->getOption('request_param_alias', null, 'q');
$urlSegments = explode('/', $_REQUEST[$rAlias]);
$urlPath = trim($urlSegments[0] . '/' . $urlSegments[1], '/');
$rewritePaths = array(
'blog/post',
'blog/archives',
'blog/category',
'blog/tag',
'news-events/media-coverage',
'news-events/media-coverage/archive',
'news-events/newsletters',
'news-events/newsletters/archive',
'partners'
);
if(in_array($urlPath, $rewritePaths)) {
$_REQUEST[$rAlias] = $urlPath;
$_REQUEST['qstring'] = str_replace($urlPath, '', $_REQUEST[$rAlias]);
}
EDIT: Found some bugs with the above code. The following seems to fix these issues and is a bit more robust:
<?php
$rAlias = $modx->getOption('request_param_alias', null, 'q');
$urlSegments = explode('/', $_REQUEST[$rAlias]);
$urlPath = trim($urlSegments[0] . '/' . $urlSegments[1] . '/' . $urlSegments[2], '/');
$rewritePatterns = array(
'/^blog\/post/' => 'blog/post',
'/^blog\/archives/' => 'blog/archives',
'/^blog\/category/' => 'blog/category',
'/^blog\/tag/' => 'blog/tag',
'/^news-events\/media-coverage/' => 'news-events/media-coverage',
'/^news-events\/media-coverage\/archive/' => 'news-events/media-coverage/archive',
'/^news-events\/newsletters/' => 'news-events/newsletters',
'/^news-events\/newsletters/archive/' => 'news-events/newsletters/archive',
'/^partners\//' => 'partners/'
);
foreach ($rewritePatterns as $pattern => $replacement) {
if (preg_match($pattern, $urlPath)){
$_REQUEST[$rAlias] = $replacement;
$_REQUEST['qstring'] = str_replace($replacement, '', $_REQUEST[$rAlias]);
}
}
God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
Nice work! It hadn’t occurred to me to go with a plugin. It certainly seems a lot more flexible. From the looks of it you don’t even need to touch .htaccess for that to work, is that right?
Quote from: lucas at May 23, 2011, 02:05 AM
From the looks of it you don’t even need to touch .htaccess for that to work, is that right?
Correct - no need to touch .htaccess.
God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
-
MODX Staff
- 10,725 Posts
FWIW, I would use this OnPageNotFound to handle your custom routing. That way the MODX routing still works and your custom handling occurs after it fails. Otherwise, you might interfere with the default MODX routing unintentionally.