SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    967
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    url rewriting for multiple directories

    I'm trying to make site.com/category/sub-category
    redirect to www.site.com/category/sub-category

    This is what I have so far:
    RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
    RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

    It works for the second level (site.com/category) but not the third (site.com/category/sub-category).

    How would you modify this to work for the third level?

    Thank You E

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,276
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Fortunately the Apache documentation talks about this exact situation.

    http://httpd.apache.org/docs/current...#canonicalhost
    "First make it work. Then make it better."

  3. #3
    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)
    Hi eruna

    First, WELCOME to SitePoint!

    Quote Originally Posted by eruna View Post
    I'm trying to make site.com/category/sub-category
    redirect to www.site.com/category/sub-category

    This is what I have so far:
    PLEASE wrap your code in [code]...[/code] tags as that preserves the code when quoting for a reply.
    Code:
    RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
    RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
    It works for the second level (site.com/category) but not the third (site.com/category/sub-category).

    How would you modify this to work for the third level?

    Thank You E
    Your code is fine as is (although you should be warned about the EVERYTHING atom).

    [standard rant #1]
    The use of "lazy regex," specifically the EVERYTHING atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
    [/standard rant #1]

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
    RewriteRule .? http://www.site.com%{REQUEST_URI} [R=301,L]
    You could, perhaps, ensure that mod_rewrite is not in the comment mode by starting with RewriteEngine on and not using (.*) to capture text already available in the {REQUEST_URI} string but you already have an escape from the loop generally created by (.*) so no problem - it WILL capture everything in the path-to-file and simply repeat forcing the www.

    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

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,276
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    [standard rant #1]
    The use of "lazy regex," specifically the EVERYTHING atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
    [/standard rant #1]
    Since the OP *did* provide an exit, and did *not* use (.*) in error, this rant seems to be unwarranted.
    "First make it work. Then make it better."

  5. #5
    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)
    Jeff,

    Did you read my post? It was provided as a warning (to the OP and others).

    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
  •