SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    Northwood, Middlesex, North-West London, U.K.
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    htaccess problem

    Hi there, thanks in advance

    I am trying to make htaccess based multiple sites on one hosting account.
    It almost works fine, except for when the user types in a page name, of a directory that is in the same part of the tree. Eg.

    If there is a dir on my domain:
    http://www.northwoodcomputerrepairs.co.uk/news/
    and the user types:
    http://www.northwoodcomputerrepairs.co.uk/news
    the browser URL, weirdly shows:
    http://www.northwoodcomputerrepairs.co.uk/ncr/news/
    but displays the page of the first URL.

    (Where /ncr is the sub-directory that this 'site' lives in)
    What I would expect here, is a 404, as there is no PAGE here called "news".

    My htaccess works for all other URL/page types.
    Htaccess is:
    RewriteCond %{REQUEST_URI} !^/ncr/
    RewriteCond %{HTTP_HOST} northwoodcomputerrepairs.co.uk$ [OR]
    RewriteCond %{HTTP_HOST} ^www.northwoodcomputerrepairs.co.uk$
    RewriteRule ^(.*)$ ncr/$1 [L]


    Any ideas? Thanks
    Avanti Computer Services
    ----------------------------------
    If you need Computer Repair or Support in Northwood, Middlesex
    contact Avanti Computer Services today!

  2. #2
    SitePoint Wizard ~ServerPoint~'s Avatar
    Join Date
    Nov 2007
    Posts
    1,813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... Maybe it worth to look at that with your web hosting provider. I believe that can help
    ServerPoint.com - a true hosting company since 1998
    Web Hosting, colocation,
    dedicated servers, Virtual Private Server (VPS) hosting

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,680
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by ~ServerPoint~ View Post
    Hmm... Maybe it worth to look at that with your web hosting provider. I believe that can help
    Nope! See below.

    Avanti-SEO,

    The problem was with your mod_rewrite (which was the reason I moved your post to the Aache forum):
    Code:
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/ncr/
    # that / will ONLY allow a match on Apache 1.x servers
    # if you're hosted on Apache 2.x, delete, otherwise, change the / to /?
    RewriteCond %{HTTP_HOST} northwoodcomputerrepairs\.co\.uk$ [NC,OR]
    # escape the dot characters when you want to match ONLY the dot character
    # domains (the {HTTP_HOST} variable) are case INsensitive so
    # you really NEED the No Case flag
    RewriteCond %{HTTP_HOST} ^www\.northwoodcomputerrepairs\.co\.uk$ [NC]
    # Ditto
    # ... or just combine them with ...
    # RewriteCond %{HTTP_HOST} ^(www\.)?northwoodcomputerrepairs\.co\.uk$ [NC]
    RewriteRule ^(.*)$ ncr/$1 [L]
    # which would "optimized" by using ...
    # RewriteRule .? ncr%{REQUEST_URI} [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 Enthusiast
    Join Date
    Mar 2009
    Location
    Northwood, Middlesex, North-West London, U.K.
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Dklynn

    Thank you for your excellent post.
    - Unfortunatley I can't get it to work.
    My htaccess is now:

    RewriteCond %{REQUEST_URI} !^ncr
    RewriteCond %{HTTP_HOST} ^(www\.)?northwoodcomputerrepairs\.co\.uk$ [NC]
    RewriteRule .? ncr%{REQUEST_URI} [L]

    I am still getting the url re-written to include the /ncr/
    http://www.northwoodcomputerrepairs.co.uk/ncr/news/

    Any thoughts?
    Apache version 2.2.15
    Avanti Computer Services
    ----------------------------------
    If you need Computer Repair or Support in Northwood, Middlesex
    contact Avanti Computer Services today!

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,680
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Avanti-SEO,

    Yes, please specify in words EXACTLY what you want to do (I'm DENSE today - as if that's anything unusual ). What I'd posted above was meant to redirect non-ncr requests of that domain to the ncr/{request} without displaying the redirection (to display the redirection, add R=301 to the Last flag - this is a great troubleshooting technique so use it freely and remove it when you DON'T want to display the redirection).

    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 Enthusiast
    Join Date
    Mar 2009
    Location
    Northwood, Middlesex, North-West London, U.K.
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dklynn

    Thank you again.

    I want all pages for the domain:
    www.northwoodcomputerrepairs.co.uk directed to my {root}/ncr directory, but without the /ncr directory name ever appearing in the browser (or Search Engine) URL.

    So I want someone typing in:
    www.northwoodcomputerrepairs.co.uk to get {root}/ncr/index.html
    and
    www.northwoodcomputerrepairs.co.uk/news or
    www.northwoodcomputerrepairs.co.uk/news/
    to get {root}/ncr/news/index.html

    What happens with ALL the htaccess variations we've spoken about, is that:
    www.northwoodcomputerrepairs.co.uk/ works as intended (/ncr not visible in URL)
    www.northwoodcomputerrepairs.co.uk/news/ works
    www.northwoodcomputerrepairs.co.uk/news kinda works....

    When the above is typed in, the Address bar then shows:
    http://www.northwoodcomputerrepairs.co.uk/ncr/news/
    Though the page displayed is {root}/ncr/news/index.html

    The same problem applies to all 'slugs' (file\directory name in url), where the directory name is typed in the url without the trailing /.
    So for example:
    http://www.northwoodcomputerrepairs.co.uk/about and
    http://www.northwoodcomputerrepairs.co.uk/about/ should go to
    {root}/ncr/about/index.html and show
    http://www.northwoodcomputerrepairs.co.uk/about/ in the address bar

    But http://www.northwoodcomputerrepairs.co.uk/about shows the desired page, but shows this in the address bar.
    http://www.northwoodcomputerrepairs.co.uk/ncr/about/

    Interestingly, when I set that last flag to R=301, this behavior happens to all pages on that domain.

    Thank you again
    Avanti Computer Services
    ----------------------------------
    If you need Computer Repair or Support in Northwood, Middlesex
    contact Avanti Computer Services today!

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,680
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Avanti-SEO,

    Uh, I read that as redirect EVERYTHING to the ncr subdirectory without displaying it. The easiest (BEST) way to do that is to redefine the {DocumrntRoot} so that's what I have to advise (first and foremost).

    After that, I'd change the



    Code:
    RewriteEngine on
    # remove ncr IF it's not a subrequest (already redirected)
    RewriteRule ^ncr/(.*)$ $1 [NS,R=301,L]
    
    # redirect EVERYTHING to ncr subdirectory
    # prevent loop with condition
    RewriteCond %{REQUEST_URI} !^ncr/
    # redirect request to the ncr subdirectory
    RewriteRule .? ncr%{REQUEST_URI} [L]
    If your Apache is directed to add the trailing slash AND DirectoryIndex, those should be done by the core BEFORE mod_rewrite gets involved. However, I'd advise NOT adding a trailing slash on a filename.ext "just for fun" (if for no other reason that it upsets the browser when it makes its relative link requests).

    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

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    Northwood, Middlesex, North-West London, U.K.
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there Dklynn

    Thank you again for your efforts and time taken.

    Despite our good efforts, I think we are still having 'commincation problems' (probably my fault!).

    You were, I think, very close to the mark with your first interpretation of my needs. And your first solution worked fine except for "

    www.northwoodcomputerrepairs.co.uk/news/ works
    www.northwoodcomputerrepairs.co.uk/news kinda works....

    When the above is typed in, the Address bar then shows:
    http://www.northwoodcomputerrepairs.co.uk/ncr/news/
    "

    To clairfy (maybe I should have mentioned this to begin with, but I didn't want to include 'unecassary' information and complicate matters): I have a basic 'virtual hosting' account. I can't change any but the most basic envirmoent vairables, through cPanel (a cut down version I think) (changing document root is a no-no).
    I have one hosting account, with multiple 'htaccess-based sites', each within it's own sub-dir of root. {Root}\NCR is one, but there are others.
    So I could not have EVERYTHING go to {Root}\NCR , as it would mess-up the other domains. I simply want all northwoodcomputerrepairs.co.uk pages to be taken from the ncr dir as if ncr was the root directory.
    This all works fine (invisible to the end-user/search engine), except for the stated problem.

    - I had tried my own code to remove /ncr, from the URL is if already present, but both mine and your most recent code (the "RewriteRule ^ncr/(.*)$ $1 [NS,R=301,L]" line specifically.), puts the server/browser in an infinite loop.

    I'm sad to say, I can't fully understand the syntax of your code, but if I remove the "RewriteRule ^ncr/(.*)$ $1 [NS,R=301,L]", and just use:
    RewriteCond %{REQUEST_URI} !^ncr/
    # redirect request to the ncr subdirectory
    RewriteRule .? ncr%{REQUEST_URI} [L]

    Then I get no loop, but a page titled "Internal Server Error" which mentions at the bottom "Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request."

    Could it be that the problem is not shoddy htaccess commands, but a bug in the Server setup? Any other ideas?
    I totally understand if you're tired of this now....
    Avanti Computer Services
    ----------------------------------
    If you need Computer Repair or Support in Northwood, Middlesex
    contact Avanti Computer Services today!

  9. #9
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,680
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Avanti-SEO,

    My "communication problems" are generally that I explain how the watch works, not the time (as asked)! I'll accept the justifiable accusation of "pedantic."

    Okay, correct except for the news subdirectory.
    Code:
    RewriteEngine on
    # remove ncr IF it's not a subrequest (already redirected)
    RewriteRule ^ncr/(.*)$ $1 [NS,R=301,L]
    
    # redirect EVERYTHING to ncr subdirectory
    # prevent loop with condition
    RewriteCond %{REQUEST_URI} !^ncr/
    # prevent redirection of the news subdirectory
    RewriteCond %{REQUEST_URI} !^news
    # redirect request to the ncr subdirectory
    RewriteRule .? ncr%{REQUEST_URI} [L]
    The problem with the news/ vs news is that your browser doesn't know it's in a different directory level so your support files will be missing with one of these links UNLESS you use one of the two solutions in my signature's tutorial.

    The ErrorDocument statement is ErrorDocument{space}{error code}{space}{ABSOLUTE redirection}. Any deviation from that will generate your 500 error.

    Regards,

    DK

    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

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    Northwood, Middlesex, North-West London, U.K.
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dklynn

    Thank you again for your time.

    I have found a partial solution, that never shows /ncr/ in address bar, always shows the corrects {root}/ncr/ page, issues 301 redirects correctly in every case (I checked using livehttpheaders), but unfortunatly requires a mannual line enrty, for each sub-directory in the site (rather than a single Rewrite line that deals with all site directories):

    RewriteCond %{REQUEST_URI} !^/?ncr/?
    RewriteCond %{HTTP_HOST} ^www\.northwoodcomputerrepairs\.co\.uk$
    RewriteRule ^(.*)$ ncr/$1 [NC,L]
    RewriteRule ^ncr\/news$ http://%{HTTP_HOST}\/news\/ [R=301,L]

    RewriteRule ^ncr\/news\/index\.html$ http://%{HTTP_HOST}\/news\/ [R=301,L]

    The last 2 lines rewrite the URLs www.northwoodcomputerrepairs.co.uk/news and http://www.northwoodcomputerrepairs....ews/index.htmland 301 redirects them to www.northwoodcomputerrepairs.co.uk/news/ in the browser address bar (the browser never shows /ncr/), while displaying the page {root}/ncr/news/index.html

    It's worth noting, that if I replace the last 2 lines with this:
    RewriteRule ^ncr\/?(.*)$ "http\:\/\/www\.northwoodcomputerrepairs\.co\.uk\/$1" [R=301,L]
    In an attempt to have EVERY sub-directory automatically 'behave', without needing an individual htaccess line, then the site-browser gets in to an infinite loop and fails.

    Any idea how I can avoid need individual entries for each sub-dir?

    I certainly couldn't have got this far without you. Thank you again
    Avanti Computer Services
    ----------------------------------
    If you need Computer Repair or Support in Northwood, Middlesex
    contact Avanti Computer Services today!

  11. #11
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,680
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Avanti-SEO,

    I've only got time for a quick answer just now - will get back later.

    I'm concerned about your code (please use [ code ] ... [ /code ] to enclose your code) - specifically, escaping anything and everything in sight! Changed (and commented):
    Code:
    # If you're hosted on Apache 2.x, omit the /? after ^
    # I've removed them here because I don't think there
    # are many Apache 1.x's in the wild any longer
    
    RewriteCond %{REQUEST_URI} !^ncr/?
    RewriteCond %{HTTP_HOST} northwoodcomputerrepairs\.co\.uk$ [NC]
    # No case flag belongs with {HTTP_HOST},
    # NOT with {REQUEST_URI}, i.e., RewriteRule!
    RewriteRule ^(.*)$ ncr/$1 [L]
    
    RewriteRule ^ncr/news$ http://%{HTTP_HOST}/news/ [R=301,L]
    RewriteRule ^ncr/news/index\.html$ http://%{HTTP_HOST}/news/ [R=301,L]
    Quote Originally Posted by A-SEO
    The last 2 lines rewrite the URLs www.northwoodcomputerrepairs.co.uk/news and http://www.northwoodcomputerrepairs....ews/index.htmland 301 redirects them to www.northwoodcomputerrepairs.co.uk/news/ in the browser address bar (the browser never shows /ncr/), while displaying the page {root}/ncr/news/index.html
    Obviously, that shouldn't be happening - but it may have been caused by requiring the www. in the first mod_rewrite block (I've made it optional).
    Quote Originally Posted by A-SEO
    Any idea how I can avoid need individual entries for each sub-dir?
    How many subdirectories skip the redirection? How many don't? It's a matter of specifying one or the other (or some distinctive characteristic) to allow mod_rewrite to "read your mind."

    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
  •