Page 1 of 1

mod_rewrite and .html

Posted: Thu Feb 24, 2005 7:33 pm
by Tys
I would like to remove the .html off the end of my entry urls. For me the whole point to using mod_rewrite is to make my urls pretty, tossing on a .html on the end defeats that for me.

ie, it currently is:
http://domain.com/archives/57-entry.html

and I would like just:
http://domain.com/archives/57-entry

I have tried mucking around with the .htaccess rules with no luck. http://domain.com/archives/57-entry will just show all entries instead of entry 57. Any idea how to accomplish this?

Re: mod_rewrite and .html

Posted: Sun Feb 27, 2005 12:29 am
by garvinhicking
Okay. I'll tell you the modifications for Serendipity 0.8. They basically also apply to 0.7, but in 0.7 you will find the files in different directories.

1. Open your include/tpl/htaccess_rewrite.tpl file
2. Modify those patterns:

Code: Select all

RewriteRule ^{PAT_ARCHIVES} {indexFile}?url=/{PATH_ARCHIVES}/$1.html [L,QSA]
RewriteRule ^([0-9]+)[_\-][0-9a-z_\-]*\.html {indexFile}?url=$1-article.html [L,NC,QSA]
to this:

Code: Select all

RewriteRule ^{PAT_ARCHIVES} {indexFile}?url=/{PATH_ARCHIVES}/$1 [L,QSA]
RewriteRule ^([0-9]+)[_\-][0-9a-z_\-]*{indexFile}?url=$1-article [L,NC,QSA]
2. Open your serendipity_config.inc.php file. Change those constants:

Code: Select all

@define('PERM_ARCHIVES', '%id%-%title%.html');
@define('PAT_ARCHIVES', '@/'.PATH_ARCHIVES.'([/A-Za-z0-9]+)\.html@');
@define('PAT_COMMENTSUB', '@/([0-9]+)[_\-][' . PAT_FILENAME . ']*\.html@i');
into this:

Code: Select all

@define('PERM_ARCHIVES', '%id%-%title%');
@define('PAT_ARCHIVES', '@/'.PATH_ARCHIVES.'([/A-Za-z0-9]+)@');
@define('PAT_COMMENTSUB', '@/([0-9]+)[_\-][' . PAT_FILENAME . ']*@i');
If you're using 0.7 you will also need to open serendipity_functions.inc.php and change function serendipity_archiveURL() into:

Code: Select all

function serendipity_archiveURL($id, $title, $key = 'baseURL', $checkrewrite = true) {
global $serendipity;

    return $serendipity[$key] . ($checkrewrite && $serendipity['rewrite'] == 'none' ? $serendipity['indexFile'] . '?/' : '') . PATH_ARCHIVES . '/' . (int)$id . '-' . serendipity_makeFilename($title);
}
3. Then go to Serendipity configuration, set your URL Rewriting to None, Save, set it to 'mod_rewrite' and save again. This will recreate your .htaccess with your new htaccess_rewrite.tpl settings.

Then it should work. Note that however searchengines like google will now score your URLs lower, because at the moment .html files have higher precedence than a path name.

Regards,
Garvin

Posted: Mon Feb 28, 2005 6:50 pm
by Tys
Thank you, I was just trying to change them in the .htaccess file, this clears things up.

As for the .html and google, good point, I can see why the majority of people might want the .html.