.htaccess rewrite rule to remove trailing slash

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
docbrown
Regular
Posts: 10
Joined: Wed Aug 02, 2006 11:29 am
Location: Erfurt / Thüringen / Germany

.htaccess rewrite rule to remove trailing slash

Post by docbrown »

Hi!

I'm still searching for a solution to let serendipity get rid of trailing slashes in incoming urls. Is it better to handle it with .htaccess (if yes how?) or to edit index.php and the url rewriting?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: .htaccess rewrite rule to remove trailing slash

Post by garvinhicking »

Hi!

Where exactly, in which permalink do you want to remove a trailing slash?

Trailing slashes are actually better both in terms of HTTP lookups and in terms of SEO/Google.

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
docbrown
Regular
Posts: 10
Joined: Wed Aug 02, 2006 11:29 am
Location: Erfurt / Thüringen / Germany

Post by docbrown »

Okay, but: I have a problem with old and existing links to my page. A few month ago, i made an "upgrade" from wordpress to serendipity. Wordpress was using links with a trailing slash at the end. Thats why, differnt pages know my links with that slash. I was just trying to handly that small problem. How to teach serendipity to accept links (means show the articles) with and without a trailing slash...
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

If you remove the %id% from permalinks, you have little options to accept the permalink, because s9y will match only the full link against the 'serendipity_permalinks' DB table. You would need to patch serendipity to always insert two entries into that DB table: One with slash, and one without.

If you howeve rkeep the %id% value in your permalink, you can simply edit your .htaccess file to replace "/" with "/?" in the permalink regex.

The other option you have is to generally use mod_rewrite to redirect all your trailing slash URLs with no-traling-slash URLs. If you can give us your precise .htaccess content, I can have a try at modifying that for you.

Regards,
GArvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
docbrown
Regular
Posts: 10
Joined: Wed Aug 02, 2006 11:29 am
Location: Erfurt / Thüringen / Germany

Post by docbrown »

garvinhicking wrote:If you howeve rkeep the %id% value in your permalink, you can simply edit your .htaccess file to replace "/" with "/?" in the permalink regex.

The other option you have is to generally use mod_rewrite to redirect all your trailing slash URLs with no-traling-slash URLs. If you can give us your precise .htaccess content, I can have a try at modifying that for you.
That would be great. Here it is.

Code: Select all

# BEGIN s9y
ErrorDocument 404 /index.php
DirectoryIndex /index.php

RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[0-9a-z\.\_!;,\+\-]+) index.php?/$1 [NC,L,QSA]
RewriteRule ^(authors/([0-9]+)-[0-9a-z\.\_!;,\+\-]+) index.php?/$1 [NC,L,QSA]
RewriteRule ^(feeds/categories/([0-9;]+)-[0-9a-z\.\_!;,\+\-]+\.rss) index.php?/$1 [NC,L,QSA]
RewriteRule ^(feeds/authors/([0-9]+)-[0-9a-z\.\_!;,\+\-]+\.rss) index.php?/$1 [NC,L,QSA]
RewriteRule ^(categories/([0-9;]+)-[0-9a-z\.\_!;,\+\-]+) index.php?/$1 [NC,L,QSA]
RewriteRule ^archives([/A-Za-z0-9]+)\.html index.php?url=/archives/$1.html [NC,L,QSA]
RewriteRule ^([0-9]+)[_\-][0-9a-z_\-]*\.html index.php?url=$1-article.html [L,NC,QSA]
RewriteRule ^feeds/(.*) index.php?url=/feeds/$1 [L,QSA]
RewriteRule ^unsubscribe/(.*)/([0-9]+) index.php?url=/unsubscribe/$1/$2 [L,QSA]
RewriteRule ^approve/(.*)/(.*)/([0-9]+) index.php?url=approve/$1/$2/$3 [L,QSA]
RewriteRule ^delete/(.*)/(.*)/([0-9]+) index.php?url=delete/$1/$2/$3 [L,QSA]
RewriteRule ^(admin|entries)(/.+)? index.php?url=admin/ [L,QSA]
RewriteRule ^archive/? index.php?url=/archive [L,QSA]
RewriteRule ^(index|atom[0-9]*|rss|b2rss|b2rdf).(rss|rdf|rss2|xml) rss.php?file=$1&ext=$2
RewriteRule ^(plugin|plugin)/(.*) index.php?url=$1/$2 [L,QSA]
RewriteRule ^search/(.*) index.php?url=/search/$1 [L,QSA]
RewriteRule ^(serendipity\.css|serendipity_admin\.css) index.php?url=/$1 [L,QSA]
RewriteRule ^index\.(html?|php.+) index.php?url=index.html [L,QSA]
RewriteRule ^htmlarea/(.*) htmlarea/$1 [L,QSA]
RewriteRule (.*\.html?) index.php?url=/$1 [L,QSA]

<Files *.tpl.php>
    deny from all
</Files>

<Files *.tpl>
    deny from all
</Files>

<Files *.sql>
    deny from all
</Files>

<Files *.inc.php>
    deny from all
</Files>

<Files *.db>
    deny from all
</Files>

# END s9y
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Can you try if this works:

Code: Select all

# BEGIN s9y
ErrorDocument 404 /index.php
DirectoryIndex /index.php

RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]{4}/[0-9]{1,2}/[0-9]{1,2}/[0-9a-z\.\_!;,\+\-]+)/? index.php?/$1 [NC,L,QSA]
RewriteRule ^(authors/([0-9]+)-[0-9a-z\.\_!;,\+\-]+)/? index.php?/$1 [NC,L,QSA]
RewriteRule ^(feeds/categories/([0-9;]+)-[0-9a-z\.\_!;,\+\-]+\.rss) index.php?/$1 [NC,L,QSA]
RewriteRule ^(feeds/authors/([0-9]+)-[0-9a-z\.\_!;,\+\-]+\.rss) index.php?/$1 [NC,L,QSA]
RewriteRule ^(categories/([0-9;]+)-[0-9a-z\.\_!;,\+\-]+)/? index.php?/$1 [NC,L,QSA]
RewriteRule ^archives([/A-Za-z0-9]+)\.html index.php?url=/archives/$1.html [NC,L,QSA]
RewriteRule ^([0-9]+)[_\-][0-9a-z_\-]*\.html index.php?url=$1-article.html [L,NC,QSA]
RewriteRule ^feeds/(.*) index.php?url=/feeds/$1 [L,QSA]
RewriteRule ^unsubscribe/(.*)/([0-9]+) index.php?url=/unsubscribe/$1/$2 [L,QSA]
RewriteRule ^approve/(.*)/(.*)/([0-9]+) index.php?url=approve/$1/$2/$3 [L,QSA]
RewriteRule ^delete/(.*)/(.*)/([0-9]+) index.php?url=delete/$1/$2/$3 [L,QSA]
RewriteRule ^(admin|entries)(/.+)? index.php?url=admin/ [L,QSA]
RewriteRule ^archive/? index.php?url=/archive [L,QSA]
RewriteRule ^(index|atom[0-9]*|rss|b2rss|b2rdf).(rss|rdf|rss2|xml) rss.php?file=$1&ext=$2
RewriteRule ^(plugin|plugin)/(.*) index.php?url=$1/$2 [L,QSA]
RewriteRule ^search/(.*) index.php?url=/search/$1 [L,QSA]
RewriteRule ^(serendipity\.css|serendipity_admin\.css) index.php?url=/$1 [L,QSA]
RewriteRule ^index\.(html?|php.+) index.php?url=index.html [L,QSA]
RewriteRule ^htmlarea/(.*) htmlarea/$1 [L,QSA]
RewriteRule (.*\.html?) index.php?url=/$1 [L,QSA]

<Files *.tpl.php>
    deny from all
</Files>

<Files *.tpl>
    deny from all
</Files>

<Files *.sql>
    deny from all
</Files>

<Files *.inc.php>
    deny from all
</Files>

<Files *.db>
    deny from all
</Files>

# END s9y
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
docbrown
Regular
Posts: 10
Joined: Wed Aug 02, 2006 11:29 am
Location: Erfurt / Thüringen / Germany

Post by docbrown »

Still the same. Everything ok without the slash and an article not found with the slash.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Could you tell me the URL to your blog?

Maybe you can edit your include/functions_permalinks.inc.php file and replace this:

Code: Select all

    $PAT['UNSUBSCRIBE']              = '@/'  . $serendipity['permalinkUnsubscribePath'].'/(.*)/([0-9]+)@';
    $PAT['APPROVE']                  = '@/'  . $serendipity['permalinkApprovePath'].'/(.*)/(.*)/([0-9]+)@';
    $PAT['DELETE']                   = '@/'  . $serendipity['permalinkDeletePath'].'/(.*)/(.*)/([0-9]+)@';
    $PAT['ARCHIVES']                 = '@/'  . $serendipity['permalinkArchivesPath'].'([/A-Za-z0-9]+)\.html@';
    $PAT['FEEDS']                    = '@/'  . $serendipity['permalinkFeedsPath'].'/@';
    $PAT['ADMIN']                    = '@/(' . $serendipity['permalinkAdminPath'] . '|entries)(/.+)?@';
    $PAT['ARCHIVE']                  = '@/'  . $serendipity['permalinkArchivePath'] . '/?@';
    $PAT['CATEGORIES']               = '@/'  . $serendipity['permalinkCategoriesPath'].'/([0-9;]+)@';
    $PAT['PLUGIN']                   = '@/('  . $serendipity['permalinkPluginPath'] . '|plugin)/(.*)@';
    $PAT['SEARCH']                   = '@/'  . $serendipity['permalinkSearchPath'] . '/(.*)@';
    $PAT['COMMENTS']                 = '@/'  . $serendipity['permalinkCommentsPath'] . '/(.*)@';
    $PAT['PERMALINK']                = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkStructure'], 'entry') . '@i';
    $PAT['PERMALINK_CATEGORIES']     = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkCategoryStructure'], 'category') . '@i';
    $PAT['PERMALINK_FEEDCATEGORIES'] = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkFeedCategoryStructure'], 'category') . '@i';
    $PAT['PERMALINK_FEEDAUTHORS']    = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkFeedAuthorStructure'], 'author') . '@i';
    $PAT['PERMALINK_AUTHORS']        = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkAuthorStructure'], 'author') . '@i';
with this:

Code: Select all

    $PAT['UNSUBSCRIBE']              = '@/'  . $serendipity['permalinkUnsubscribePath'].'/(.*)/([0-9]+)/?@';
    $PAT['APPROVE']                  = '@/'  . $serendipity['permalinkApprovePath'].'/(.*)/(.*)/([0-9]+)/?@';
    $PAT['DELETE']                   = '@/'  . $serendipity['permalinkDeletePath'].'/(.*)/(.*)/([0-9]+)/?@';
    $PAT['ARCHIVES']                 = '@/'  . $serendipity['permalinkArchivesPath'].'([/A-Za-z0-9]+)\.html@';
    $PAT['FEEDS']                    = '@/'  . $serendipity['permalinkFeedsPath'].'/?@';
    $PAT['ADMIN']                    = '@/(' . $serendipity['permalinkAdminPath'] . '|entries)(/.+)?@';
    $PAT['ARCHIVE']                  = '@/'  . $serendipity['permalinkArchivePath'] . '/?@';
    $PAT['CATEGORIES']               = '@/'  . $serendipity['permalinkCategoriesPath'].'/([0-9;]+)/?@';
    $PAT['PLUGIN']                   = '@/('  . $serendipity['permalinkPluginPath'] . '|plugin)/(.*)@';
    $PAT['SEARCH']                   = '@/'  . $serendipity['permalinkSearchPath'] . '/(.*)@';
    $PAT['COMMENTS']                 = '@/'  . $serendipity['permalinkCommentsPath'] . '/(.*)@';
    $PAT['PERMALINK']                = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkStructure'], 'entry') . '/?@i';
    $PAT['PERMALINK_CATEGORIES']     = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkCategoryStructure'], 'category') . '/?@i';
    $PAT['PERMALINK_FEEDCATEGORIES'] = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkFeedCategoryStructure'], 'category') . '/?@i';
    $PAT['PERMALINK_FEEDAUTHORS']    = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkFeedAuthorStructure'], 'author') . '/?@i';
    $PAT['PERMALINK_AUTHORS']        = '@'   . serendipity_makePermalinkRegex($serendipity['permalinkAuthorStructure'], 'author') . '/?@i';
Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
docbrown
Regular
Posts: 10
Joined: Wed Aug 02, 2006 11:29 am
Location: Erfurt / Thüringen / Germany

Post by docbrown »

garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Okay, I just recreated the problem on my local setup.

It actually takes a bit more of patching to succeed. At this point of the serendipity release cycle I cannot add such a possibly intrusive patch to the distribution, but will do so with Serendipity 1.2

Meanwhile, you can use this patch:

http://svn.berlios.de/viewcvs/serendipi ... ks.inc.php

Apply those changes and then UNCOMMENT (remove the "//" in front of) the sections where it says "Patch for 1.2". When those lines are enabled, permalinks with an ending "/" are properly checked.

You will then also either need to resave your serendipity configuration so that an updated .htaccess will be written, or you keep the manual changes you applied earlier on with my instructions of this thread.

HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
docbrown
Regular
Posts: 10
Joined: Wed Aug 02, 2006 11:29 am
Location: Erfurt / Thüringen / Germany

Post by docbrown »

garvinhicking wrote:It actually takes a bit more of patching to succeed. At this point of the serendipity release cycle I cannot add such a possibly intrusive patch to the distribution, but will do so with Serendipity 1.2
Ack. Looking forward to version 1.2. :-)
garvinhicking wrote:Meanwhile, you can use this patch:

Apply those changes and then UNCOMMENT (remove the "//" in front of) the sections where it says "Patch for 1.2". When those lines are enabled, permalinks with an ending "/" are properly checked.

You will then also either need to resave your serendipity configuration so that an updated .htaccess will be written, or you keep the manual changes you applied earlier on with my instructions of this thread.
Very well. I'll do so. Thank u very much for your help.
Post Reply