SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirecting through .htaccess

    I want to redirect a url http://abc.xyz.com/123 to http://www.xyz.com. So the file that will be accessed will be index.php of xyz.com. That index.php should have available to it both abc and 123 (so 123 will be $_SERVER['PHP_SELF'], and abc will come out of $_SERVER['HTTP_HOST']) to enable appropriate processing.

    http://abc.xyz.com already goes to http://www.xyz.com (though configuration by my service provider), and abc can be extracted out of $_SERVER['HTTP_HOST']. However, http://abc.xyz.com/123 seems to be looking for a folder called 123 in the root directory of xyz.com, and is showing up a 404 error when it cannot find any.

    Can someone kindly give me the line(s) to be put in .htaccess to enable http://abc.xyz.com/123 to call index.php of http://www.xyz.com (with 123 being available though $_SERVER['PHP_SELF']? I cannot seem to get it to work.

    Thank you very much for your time!

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

    What you're asking for ($_SERVER['PHP_SELF'] to be something other than 123/ or index.php if your host redirected as you requested - they did not) can't be done BECAUSE $_SERVER['PHP_SELF] is the name of the current script being run. Therefore, the best you can do is to get index.php out of that (when the host redirects correctly).

    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 Enthusiast
    Join Date
    Jun 2007
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, DK. I _do_ want $_SERVER[PHP_SELF] to be 123. I just meant I did not want that value to be lost in the rewriting.

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

    When you redirect, it IS lost. Redirecting from a subdomain to a domain can be done to the previous {REQUEST_URI} or to the DirectoryIndex or any other script (page) but PHP's function which fetches the value of PHP_SELF can only do that, examine the NAME of the script which is calling it, i.e., your host redirected you to 123/ and, if that directory existed, Apache would serve the DirectoryIndex (123/index.php?) and the $_SERVER['PHP_SELF'] would be index.php in the 123 directory.

    If that's unacceptable, you can redirect to the xyz.com DirectoryIndex with a query string which identifies that it came from the abc subdomain - but that wasn't what you asked.

    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 Enthusiast
    Join Date
    Jun 2007
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, DK. Perhaps I did not communicate well enough - I am not redirecting, I am only rewriting. I am not too good at this, but I am assuming that if I rewrite http://abc.xyz.com/123 to http://www.xyz.com/index.php, then for index.php the $_SERVER variables will come from http://abc.xyz.com/123, unlike in the case of actual redirecting (by, say, using the header function of PHP). Kindly let me know if I am totally wrong in all this...

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,692
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)
    knkk,

    As explained TWICE above, you ARE totally wrong on that. It's a PHP thing and I wouldn't want them to do anything differently.

    That said, you MAY be able to get around the PHP problem by accessing {THE_REQUEST} (which has a lot of garbage besides the URI which you'll need to strip out). However, with a 301 redirect like you're doing, that may not work either.

    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

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, dklynn. I finally implemented this solution for my subdomain subdirectory redirection problem above:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URL} !=/favicon.ico
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

    q will contain the $_SERVER['PHP_SELF'] I mentioned (123) in my original post...


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
  •