SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Renamed directories to sub-domain

    Good day,

    I have CMS with SEO friendly URL rewrite enabled that generates URLs lets say www.domain.com/urllevel1/urllevel2

    And I have created sub-domain lets say sub1.domain.com

    Now, I want some of these generated URLs (example www.domain.com/urllevel1/...etc) to be browsed as sub1.domain.com/urllevel1/...etc

    Any idea?

    Best regards,
    Ahmed.

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

    Sure! Since a subdomain is normally "pointed" at a subdirectory of the domain, it's safe to simply use mod_rewrite in the domain's .htaccess to check for the existance of the urllevel1 directory then redirect to HTTP://SUB1.domain.com (with the remainder of the original URI after urllevel1/ has been removed).

    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
    Feb 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Ahmed,

    Sure! Since a subdomain is normally "pointed" at a subdirectory of the domain, it's safe to simply use mod_rewrite in the domain's .htaccess to check for the existance of the urllevel1 directory then redirect to HTTP://SUB1.domain.com (with the remainder of the original URI after urllevel1/ has been removed).

    Regards,

    DK
    Thank DK for the prompt response! what you have said is exactly what I'm looking for...

    Currently, in my domain root .htaccess I have the follow rules to enable the SEO URLs

    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}/index.html !-f
    RewriteCond %{REQUEST_FILENAME}/index.php !-f
    RewriteRule . index.php [L]
    </IfModule>
    What do I need to add here in order to achieve my theory?

    Thanks in advance,
    A

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

    No problem, mate!

    Comments on your code:

    Code:
    <IfModule mod_rewrite.c>
    
    [standard rant #4]
    The definition of insanity is to repeatedly do the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> ... </IfModule> wrapper is the same thing in the webmaster world. DON'T ACT INSANE! If you don't know whether a module is enabled, run the test ONCE (without the wrapper) then delete it permanently as it is EXTREMELY wasteful of Apache's resources (and should NEVER be allowed on a shared server).
    [/standard rant #4] Uh, simply test once to see whether your mod_rewrite is enabled then delete the <IfModule> and </IfModule> wrapper code.
    RewriteEngine On RewriteBase / Remove this as it's just plain not needed (and can confuse you if you have a mod_alias redirection). RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} /index.html !-f Don't forget the space after the variable you're testing against - then remove the leading / for Apache 2.x servers (and escape the dot character in the regex). RewriteCond %{REQUEST_FILENAME} /index.php !-f Ditto the above. RewriteRule . index.php [L] What about the domain-only request? You've required a single character (the / doesn't count) so you've got to rely on the DirectoryIndex to request index.php. Use a ? after the dot (optional character) so the regex will ALWAYS be true and the redirection will take effect. </IfModule> As above.
    What do I need to add here in order to achieve my theory?
    Is this what your new CMS requires? If so, IMMEDIATELY after the RewriteEngine on ...

    Code:
    # in domain.com's Document Root (so sub1.domain.com can't access it)
    
    # redirect to HTTP://SUB1.domain.com (with the remainder of the original URI after urllevel1/ has been removed)
    
    RewriteRule ^subdir1/(.*)$ http://sub1.domain.com/$1 [R=301,L]
    
    # Here, you DO want to capture the entire URI after the subdir so (.*) 
    will do that for you and the domain change will prevent looping.
    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
    Feb 2014
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks dklynn!

    The
    Code:
    RewriteRule ^subdir1/(.*)$ http://sub1.domain.com/$1 [R=301,L]
    did the trick and redirected my virtual pages to the sub-domain.

    However, it gave me 500 error everytime I browse any of the links or endless loop

    For better understanding on what's going go

    Please login to the test environment located here www.artinglow.net

    Username: guest
    Password: guest123

    Click on "Section" this should take you to www.artinglow.net/section/ but after applying the rewrite rule as follows:

    Code:
    RewriteRule ^section/(.*)$ http://section.artinglow.net/$1 [R=301,L]
    It will show you http://section.artlinglow.net

    and the same should go for "Sub-section"...

    Now, to bring the actual pages up..and remove these errors..

    Best regards,
    A

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Hi tt2!

    Glad to hear that it's working for you ... but the question you've asked makes me wonder:

    Quote Originally Posted by ta2ta2 View Post
    The
    Code:
    RewriteRule ^subdir1/(.*)$ http://sub1.domain.com/$1 [R=301,L]
    did the trick and redirected my virtual pages to the sub-domain.

    However, it gave me 500 error everytime I browse any of the links or endless loop

    This should NOT have happened as we've only stripped the subdir1/ from the path/to/file (where path = ^subdir1).

    Asking for subdir1 (from domain), the server should serve the specified DirectoryIndex so, have you set the DirectoryIndex in the subdir1 directory? Remember, if not, the server's default (index.html?) will be served (or the ErrorDocument if you're not using index.html).

    Even if you're using absolute links (to the domain/subdir1/path/to/file), the path/to/file within the subdir1 should be the redirection (within the subdomain).

    Therefore, please show ALL your .htaccess as there must be something else going on.

    For better understanding on what's going go

    Please login to the test environment located here www.artinglow.net

    Username: guest
    Password: guest123

    WARNING: placing a username and password in a public forum is NOT advisable!!! I recommend that you close this account ASAP!

    Click on "Section" this should take you to www.artinglow.net/section/ but after applying the rewrite rule as follows:

    Code:
    RewriteRule ^section/(.*)$ http://section.artinglow.net/$1 [R=301,L]
    It will show you http://section.artlinglow.net

    and the same should go for "Sub-section"...

    Now, to bring the actual pages up..and remove these errors..

    Best regards,
    A
    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
  •