Forum Moderators: phranque

Message Too Old, No Replies

subdomain via php

different folders to same subdomain

         

carthago

8:51 am on Oct 22, 2008 (gmt 0)

10+ Year Member



Hi,

I am not sure if following is possible but hope I can find some help here (if it can be done).

For my website i want to redirect pages located in different folders to the same subdomain.

www.site.com
www.site.com/folder1/pages
www.site.com/folder2/pages
www.site.com/folder3/pages

These pages i want to redirect to the same subdomainname.site.com

Thanks for the help

ag_47

1:25 am on Oct 23, 2008 (gmt 0)

10+ Year Member



Ontion 1: Slap an index.php in each of these folders with a redirect, something like
<? header("Location: subdomainname.site.com"); ?>

Option 2 (better): Use Apache + mod_rewrite;

carthago

10:09 am on Oct 23, 2008 (gmt 0)

10+ Year Member



Thank you for your reply.

Option b is what I am searching for.

On the server I have:

domain / subdomain1/different folders and pages

Now I want that:

www.domain.com/subdomain1/anyfolder/anypage

wil point to subdomain1.domain.com/anyfolder/anypage

In my .htaccess:


RewriteEngine On
Options +FollowSymlinks
RewriteBase /

RewriteCond %{HTTP_HOST} subdomain1.domain.com$
RewriteCond %{REQUEST_URI} !subdomain1/
RewriteRule ^(.*)$ subdomain1/$1

In the subdomain1 folder I included an index page

wildcard and mod_rewrite is on

-> when typing subdomain1.domain.com the index page is not found
-> when typing www.subdomain1.com the index page of subdomain1 is found

-> when surfing the website and clicking on a link that points to sudomain1 folder/pages it still shows the
www.domain.com/subdomain1/pages

How to make this work so all is pointing at subdomain1.domain.com/pages or when pages are in folder
subdomain1.domain.com/folder/pages

Thanks for the help and suggestions

g1smd

12:29 pm on Oct 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



REQUEST_URI contains the folder and filepath within the URL, not the hostname or subdomain information.

You asked for a redirect, but your code is for a rewrite.

It is important to separate out what is namespace as a URL and what is namespace as a path on the server internals.

A redirect will contain a target hostname for the redirect, and [R=301,L] to flag the redirect.

g1smd

12:35 pm on Oct 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** will point to ***

What do you mean by "will point to"?

Does the URL the user sees change, or is content delivered from the other server location without revealing what that other location is?

URLs are defined by what is in the link that the user clicks on, so once you have the redirect/rewrite working, the links on the site should then also be updated.

jdMorgan

1:25 pm on Oct 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's clarify, because I think the description of the problem may be backwards:

  • What is the URL you want to appear in the browser address bar?
  • What URL do you link to on your page(s)?
  • What is the actual internal server filepath that the URL shown in the browser should resolve to?

    I believe this will end up being the classical "Change links on pages to new URLs, rewrite new linked URLs to actual server filepaths, redirect only direct client requests for old URLs to new URLs"- type solution.

    Jim

  • carthago

    2:34 pm on Oct 23, 2008 (gmt 0)

    10+ Year Member



    Thank you g1smd & jdMorgan,

    g1smd you are right when you tell me I use the code to rewrite. That is what I want. Sorry for that.

    REQUEST_URI : htdocs/folder which has to be subdomain/here can be folders as well

    I want that visitors see subdomain.domain.com/page (or file structure)

    jdMorgan:

    - subdomain.domain.com
    - I link to the old structure www.domain.com/file/page (hope that a rewrite will do all)
    - www.domain.com/foldersubdomainname/page

    htdocs/folder i want to make subdomain/pages
    htdocs/folder i want to make subdomain/folders in the folder/pages

    Again thank you and hope my explanation i more clear now.

    carthago

    2:35 pm on Oct 23, 2008 (gmt 0)

    10+ Year Member



    The htaccess is in the htdocs folder

    g1smd

    3:21 pm on Oct 23, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    For the address to appear in the browser address bar, that is the URL that needs to be in the links on the pages. The URL in the link defines the URL.

    You will need to link to the new structure. The rewrite will NOT do what you want. A rewrite does not "change URLs". It connects a URL request clicked from a link, to a internal filepath on a server.

    jdMorgan

    3:43 pm on Oct 23, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Right, so the first step is to change the links on your pages to refer to subdomain.domain.com, then we need to clean up this code and add a second rule:

    RewriteEngine on
    Options +FollowSymLinks
    #
    # Externally redirect old subdomain-subdirectory URLs to new
    # subdomain URLs if directly requested by a client
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subdomain1/
    RewriteRule ^subdomain1/(.*)$ http://subdomain1.example.com/$1 [R=301,L]
    #
    # Internally rewrite subdomain links to subdomain-subdirectory if not already
    # done (this test is required to prevent an 'infinite' rewriting loop)
    RewriteCond %{HTTP_HOST} ^subdomain1\.domain\.com
    RewriteCond $1 !subdomain1/
    RewriteRule (.*) subdomain1/$1 [L]

    You will notice that this code "works" even without changing the links on your pages. But if you do not change the links on your pages, then every request from a client (browser or robot) following one of those links will be redirected, resulted in two requests to your server for each link followed, slowing down the "user experience" of your site, doubling of the number of "page" entries in your server access log, "pollution" of your server stats with all the extra requests, and possible risk to your pages' search engine rankings. In short, the on-page links must be changed -- You can use a "multi-file-search-and-replace" tool to do these edits more quickly.

    All changes between this code and your original code are entirely intentional.

    Jim

    [edited by: jdMorgan at 3:44 pm (utc) on Oct. 23, 2008]

    carthago

    4:12 pm on Oct 23, 2008 (gmt 0)

    10+ Year Member



    Both again thank you for the remarks and help.

    I see that all is working now.

    As I understood: I have to change ALL links which pointing to the old site.com/subdomain1folder/etc into subdomain.site.com/folders and pages

    Again you both helped me a lot.

    carthago

    4:14 pm on Oct 23, 2008 (gmt 0)

    10+ Year Member



    jdMorgan : You have a link to such 'muliti-file-search-and-replace' tool?

    g1smd

    5:12 pm on Oct 23, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Various "text editors for programmers" have such features.

    jdMorgan

    6:18 pm on Oct 23, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Just type that phrase into a search engine... They have a lot of links. :)

    Our TOS do not allow promotion of any specific products or services here on WebmasterWorld, in order to keep these forums free of spammy posts.

    Jim

    carthago

    6:57 pm on Oct 23, 2008 (gmt 0)

    10+ Year Member



    Good rule :-) Already did before asking but not know the good ones.

    g1smd

    7:14 pm on Oct 23, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    Pretty much any of those with "Type" or "Note" or "Edit" or "Pad" in the name.