Page 1 of 1
vanity URLs and associated plugin
Posted: Fri Dec 18, 2009 4:04 pm
by perlnerd
My work on a group blog version of serendipity is coming along nicely. What I'd like is the ability to give the users a vanity URL such as USERNAME.domain.com or domain.com/blog/USERNAME which maps to the appropriate s9y URL. I see that /authors/N-USERNAME is already in use, but I'd like to not have the /authors/ section or the ID number in their URL.
I figured my first step would be to nail down a proof of concept rewrite rule to take /clintg and rewrite it to /index.php?/authors/5-clintg . I added the following right after the RewriteBase rule and it doesn't appear to work.
Code: Select all
RewriteRule ^(clintg) index.php?/authors/5-$1 [NC,L,QSA]
I've also tried:
Code: Select all
RewriteRule ^authors/clintg index.php?/authors/5-clintg [NC,L,QSA]
To my mind these should be functionally the same as this later rule, minus the regex
Code: Select all
RewriteRule ^(authors/([0-9]+)-[0-9a-z\.\_!;,\+\-\%]+) index.php?/$1 [NC,L,QSA]
Assistance with this would be appreciated.
After I have that figured out, I guess I'm wondering if s9y has the ability to display posts by a user when only provided with the username? If not would it be possible to create a plugin and feed it domain.com/blog/USERNAME or USERNAME.domain.com and have it return the user ID and get us to that author's posts?
Thanks.
Clint
Re: vanity URLs and associated plugin
Posted: Sat Dec 19, 2009 4:11 pm
by garvinhicking
Hi!
Maybe this would fit your needs: Write up a simple event plugin (you seem like the guy to get kickstarted easily on the plugin API) that listens up on the 'frontend_configure' hook, and introspects the $_SERVER['HTTP_HOST'] or whatever. Depending on that variable, you simply lookup the authorid (serendipity_fetchAuthors) and then set $serendipity['GET']['viewAuthor'] to that ID.
You might need to check some additional things to see if the $GET/$POST-Vars are empty, and REQUEST_URI doesn'T contain any special URL, so that you don't fixate the viewauthor for every possible view (like calling user.domain.com/authors/5-anotheruser might fix to user instead of anotheruser, I'm not sure).
HTH,
Garvin
Re: vanity URLs and associated plugin
Posted: Sat Dec 19, 2009 4:39 pm
by perlnerd
Thanks Garvin!
That's sounds like it could work. I'll give it a try and post back.
Clint
Re: vanity URLs and associated plugin
Posted: Tue Dec 22, 2009 12:47 am
by perlnerd
So here's what I came up with.
In .htaccess I added the following mod_rewrite stanza following all the other rewrites:
Code: Select all
#we made it here so we'll make sure the request isn't an existing file or
#directory and assume it's someone trying to go to a blog's vanity URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z_-]+)$ index.php?vanityURL=$1 [L,QSA]
I created a simple plugin:
Code: Select all
...
function event_hook($event, &$bag, &$eventData) {
global $serendipity;
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
switch($event) {
case 'frontend_configure':
if(isset($_GET['vanityURL']) ){
$vanityURL = strtolower(trim($_GET['vanityURL']));
$authorInfo = serendipity_fetchAuthor($vanityURL);
if(is_array($authorInfo) && $authorInfo[0]['username'] == $vanityURL) {
//print_r($authorInfo);
$serendipity['GET']['viewAuthor'] = $authorInfo[0]['authorid'];
}
}
return true;
break;
default:
return false;
}
} else {
return false;
}
...
What I'd like is if I don't find a match for $_GET['vanityURL'] to return an error message of some sort. Is their a function to do something like that or should I just redirect to a static page with an error message?
Thanks
Clint
Re: vanity URLs and associated plugin
Posted: Tue Dec 22, 2009 2:21 am
by perlnerd
I've also noticed that serendipity_FetchAuthor() is case sensitive. I'd like UserName, username, and uSeRnAmE all to match when serendipity_fetchAuthor("username") is called.
Is it possible? Suggestions?
Thanks
Clint
Re: vanity URLs and associated plugin
Posted: Wed Dec 23, 2009 3:24 pm
by garvinhicking
Hi!
First, I'd go with a redirect to an error message URL. You could create your own event hook to handle cases like that, essentially doing the same routines that the staticpage plugin uses to display page contents. That's quite an overhead, though.
Second issue: A case invalid lookup would need to use the SQL 'LIKE' string, so you'd actually need to rip out the serendipity_fetchAuthor logic and put it within your plugin. For the future though it might be nice to add a parameter to the core sql function to support that. But since it's currently only one line of SQL query, you shouldn't be afraid to duplicate that inside your plugin.
HTH,
Garvin