Help writing mod_rewrite rule?

Discussion in 'Web Design and Development' started by cleo, Oct 22, 2008.

  1. cleo macrumors 65816

    cleo

    Joined:
    Jan 21, 2002
    Location:
    Tampa Bay Area, FL, USA
    #1
    I've recently changed blogging systems, and so my error logs are full of 404's as people follow old permalinks. (Not very "perma", huh?)

    Anyway, I know it's possible to put a rule in my htaccess file to do redirects, but my regex skills are just not up to the task.

    Old link:
    Code:
    http://gobecky.net/yyyy/mm/dd/some-words-here.php
    New link:
    Code:
    http://gobecky.net/yyyy/mm/dd/some-words-here/
    I tried:
    Code:
    RewriteRule ^([0-9]+/[0-9]+/[0-9]+/[^/]+)\.php$ $1/
    but no luck.

    Example URLs:
    Old: http://gobecky.net/2007/09/03/home-sweet-home.php
    New: http://gobecky.net/2007/09/03/home-sweet-home/

    Help?
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Try:

    Code:
    RewriteRule ^/([0-9]{4}/[0-9]{2}/[0-9]{2}/[^\.]+)\.php$ /$1/
     
  3. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #3
    Code:
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?gobecky.net/.*$ [NC]
    RewriteRule ^/([0-9]{4}/[0-9]{2}/[0-9]{2}/[^\.]+)\.php$/ http://gobecky.net/$1 [R,L]
    
    This merely takes into account the entire .htaccess, this is not a correction of angelwatt's superb resolution, it's just shows the complete ruleset I've used in the past and happens to be well documented on the Internet. My example includes case insensitive domain name checking and a redirect/last rule at the end. Untested, please note.

    -jim
     
  4. cleo thread starter macrumors 65816

    cleo

    Joined:
    Jan 21, 2002
    Location:
    Tampa Bay Area, FL, USA
    #4
    Both of those solutions make perfect sense, but neither is working for me. (And for once, my host has been sublimely unhelpful.)

    Could the fact that I'm using WordPress be causing this? (Inasmuch as the web pages are dynamically generated and don't actually live in a static directory tree.)

    Or any other insights?
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
  6. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #6
    That last link looks promising, angelwatt, but the code is circa 2005 so the OP should test with the new version of WP. Based on the comments in that link, plus the mention in the WP requirements of Apache's mod_rewrite, WP creates a bloated version of .htaccess when users turn on "Friendly URL's" option (which others refer to as Search Engine Optimization or SEO). OP, have you tried that config option first?

    How to enable it in Word Press

    If it doesn't do what you want, I found this that might be helpful:

    Various Word Press Plugins related to SEO (best: Platinum SEO pack)

    But the platinum SEO plugin which looks very powerful makes no mention of mod_rewrite, it seems to do it via PHP code. I think. Either way, its worth a shot to try - it's a highly rated plugin and works with the new version and seems to be easily upgradeable. Hope this helps!

    -jim
     

Share This Page