SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirect example.com/new-section/ url to www.example.com/new-section/

    I have some issues with my .htaccess files and my developer claims it's not possible what I want to achieve but I think it's just an attempt of avoiding more work/or he doesn't have the knowledge how to do it himself. So basically I wanted to ask around and see if this really is impossible?

    My website is divided in two different sections. There is the main page: http://www.example.com/ and then there is the new section (http://www.example.com/new-section/)

    I want http://example.com/new-section/ to redirect to http://www.example.com/new-section/

    How would I go ahead doing that?

    Please see my two htaccess files below.

    .htaccess file for root (www.example.com):
    Code:
    Options +FollowSymLinks
    Options +Indexes
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    RewriteCond %{REQUEST_URI} !\.[^./]+$
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.example.com/$1/ [R=301,L] 
    
    RewriteRule ^pages/([0-9]+)/(.*)$ index.php?page_id=$1&keyword=$2
    
    ErrorDocument 404 http://www.example.com/404page.php
    .htaccess file for sub directory (www.example.com/new-section/):
    Code:
    Options +FollowSymLinks
    Options +Indexes
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    RewriteCond %{REQUEST_URI} !\.[^./]+$
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.example.com/new-section/$1/ [R=301,L] 
    
    RewriteRule ^login/$ login.php
    RewriteRule ^contact/$ contact.php
    RewriteRule ^help/$ help.php
    
    ErrorDocument 404 http://www.example.com/404page.php
    I would really appreciate your help.

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,149
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    It's definitely possible. It looks like you're almost doing it already.

    # /new-section/.htaccess

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]


    The only reason this might not work properly is because the "new-section" path segment won't make it into the replacement URL. Rewrite rules match on a relative path, relative to the current directory of the htaccess file, so $1 won't contain any preceding parts. You'll have to write it in manually.

    # /new-section/.htaccess

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/new-section/$1 [R=301,L]


    I haven't tested it, but I believe that should solve the immediate issue.

    Going forward, I'd also suggest trying to consolidate the two htaccess files down to just the one in the root. Personally, I find that much easier to manage and maintain. Already, you can see lots of duplicated code between the two files.
    "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,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    help,

    Simply test for non-www AND new-section before your other mod_rewrite code (after RewriteEngine on, of course) IN THE DocumentRoot. Note that this will cause a loop with your force-www code so merely exclude the new-section there, too. Don't forget your No Case flags!

    .htaccess file for root (www.example.com):
    Code:
    Options +FollowSymLinks
    Options +Indexes
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{REQUEST_URI} ^new-section/
    RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{REQUEST_URI} !^new-section/
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    ...
    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 Member
    Join Date
    Jun 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did try your examples but it's quite confusing but I'll try some more later. I too think it would be beneficial only having one .htaccess file and that's what I believe most websites use.

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

    Sorry for the overly detailed explanation using your own code.

    To summarize, you've use .htaccess files in both directories whereas I prefer (not necessary, of course) to use only one. It looks like you've seen this, too.

    In both tasks (force www on the subdirectory and force non-www on non-subdirectory) where you're checking ONLY the www in the {HTTP_HOST}. Clearly, for what you're trying to do (force one in one case, the other in the other case), you must check both the {HTTP_HOST} and {REQUEST_URI}.

    Simple (and clear) enough?

    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

  6. #6
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I get what you saying now but I think I'll need a skilled htaccess programmer to sort this out, though. A little above my current skills and I don't wanna break anything.

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

    Aw, really?

    First, replace your current DocumentRoot's .htaccess with

    Code:
    # Delete this line as it should already be in the .htaccess file
    # Options +FollowSymLinks
    # NOT a good idea to show your file structure so change
    # Options +Indexes to
    Options -Indexes
    # move to top where (technique-wise) all core directive should reside
    ErrorDocument 404 http://www.example.com/404page.php
    
    RewriteEngine on
    
    # force www on new-section only
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{REQUEST_URI} ^new-section/
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    
    # force non-www on !new-section requests
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{REQUEST_URI} !^new-section/
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
    
    # force trailing /'s??? IMHO, VERY bad thing to do!
    # Don't comment this section out IF you need the trailing /'s for a VALID reason
    # RewriteCond %{REQUEST_URI} !\.[^./]+$
    # RewriteCond %{REQUEST_URI} !(.*)/$
    # RewriteCond %{REQUEST_URI} !new-section/ # added to prevent hijacking a new-section/ request
    # RewriteRule ^(.*)$ http://www.example.com/$1/ [R=301,L] 
    
    # normally, you should be checking for whether the {REQUEST_URI}
    # exists as either a file or directory, but ...
    RewriteRule ^pages/([0-9]+)/(.*)$ index.php?page_id=$1&keyword=$2 [L]
    Then delete your .htaccess in the new-section folder.

    Caveat Emptor: I don't know what your intent is with some of this code but, with my interpretation, I am offering this as I believe it's what you've asked for.

    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


Tags for this Thread

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
  •