SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Freiburg, Germany
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite question

    I've tried to get my head around this but it seems I'm stuck, some help is appreciated.

    Here's my problem: I've redsigned my site and want to redirect requests to the old URLs to the new ones.

    The old URLs were like this:

    www.domain.com/section/seven_habits.htm
    or
    www.domain.com/section/category/seven_habits.htm

    the new ones are

    www.domain.com/section/seven-habits
    or
    www.domain.com/section/category/seven-habits

    Is there a way I can rewrite old ones to new ones and send out permanent redirect headers with mod_rewrite? I want the new ones to show up in the address field of the browser.

    Thanks for any hints.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    andreas,

    Yes, quite possible!

    First, I'll assume that you've changed the links within your site. That will give you the "extensionless" URLs for your htm(l) pages.

    Now, to make things simpler (for me), I'll assume:
    1. You just want this redirection to occur in the section subdirectory
    2. You only need to redirect .htm pages
    3. You'll put the following in the .htaccess file in your root directory
    Code:
    RewriteEngine On
    RewriteRule ^section/([a-z0-9/]+)$ section/$1.htm
    This code looks at the {REQUEST_URI} (in the root directory) and checks for the section subdirectory, then checks to see that one or more lowercase letters, digits and the slash exist at the end of the string (i.e., NO dothtm). If those conditions are met (as they would be with either of your OLD examples), then the redirect is created with the .htm extension.

    Does that clarify for you?

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Freiburg, Germany
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if I understand correctly of if I explained properly. So, here again:

    The old site was static HTML pages with extensions. The new site runs on a CMS (Textpattern) which creates extensionless URLs. Additionally, I used underscores in the old filename scheme and Textpattern uses hyphens.

    I want visitors with bookmarks to old-style URLs to be redirected to the new URLs and I want the new URLs to show up in their address field.

    If I understand your example correctly, it's just the other way round than what I want. There are in fact new URLs, the old ones no longer exist once I go live but people might have bookmarks and the search engines will have old URLs in their index.

    So I want a permanent redirect but without having to specify every single URL. That's why I thought of mod_rewrite and regexs.

    I hope I explained this properly, I still have problems with the concept behind mod_rewrite, especially with old/new and which side of the expression stands for what.

    Thanks for your help.

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    andreas-kalt,

    Hmmm, that's the way that I created my response the first time then talked myself out of it .

    Without the conversion from - to _, the code would be
    Code:
    RewriteEngine On
    RewriteRule ^section/([a-z0-9/]+)\.htm$ section/$1 [L]
    Adding hyphens and converting to underscores to the above code is a little trickier:
    Code:
    RewriteEngine On
    RewriteRule ^section/([a-z0-9_/]*)-([a-z0-9_/]*)\.htm$ section/$1_$2.htm [N]
    RewriteRule ^section/([a-z0-9_/]+)\.htm$ section/$1 [L]
    The new rule will look for a hyphen amid lowercase letters, digits, underscores and slashES and replace them ONE AT A TIME with underscores then restart and look for another hyphen.

    Note 1: If you were sure that only one hyphen was in your URLs, you could drop the Next flag ([N]) (and the _'s in the first rule) and Apache would only have to make a single pass through this code.

    Note 2: Regular expressions (preg_replace) are far more powerful as they'll change every occurance of a string in another string as directed but I believe that more care is required with Apache's implimentation. My use of the Next flag may be overkill on my part but it will force Apache to restart the rewrite rules with the updated {REQUEST_URI} and that's what we need to be sure we've changed all -'s to _'s.

    By the time Apache gets to the second (and Last [L]) rule, all hyphens would have been converted to underscores and that's been added to that rule so you should be set.

    The important thing is that you can understand what's going on with the regex used so you can do it the next time.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Member
    Join Date
    Aug 2004
    Location
    Freiburg, Germany
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help, DK. In the meantime I realised that my CMS uses some rewrite rules that don't go well together with additional ones. So I stuck to normal redirects.

    But you gave me some more insight into mod_rewrite. By and by, I will certainly get my head around it. Thanks.

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    andreas-kalt,

    That's great! Glad to have been of help.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •