redirect to directory - php redirect or htaccess?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    redirect to directory - php redirect or htaccess?

    I'm trying to redirect requests for /index.php to /mydirectory/index.php

    If I use an index file in / with only this line:

    <?php header("Locatio n:http://www.mysite.com/mydirectory/"); ?>

    that seems to work.

    But can this be accomplished more efficiently with an htaccess rewrite?

    I already have this rewrite rule in my htaccess file:
    RewriteEngine on
    Options All -Indexes
    RewriteCond %{HTTP_HOST} ^www.myoldsite. com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^myoldsite.com$ [NC]
    RewriteRule ^(.*)$ http: //10.10.10.10/This just sends requests for
    myoldsite.com into hyperspace, but allows me to get email using that domain.How
    do I append to htaccess so that I can redirect requests for a specific host to a
    particular directory?Thank s in advance.

  • Alan Little

    #2
    Re: redirect to directory - php redirect or htaccess?

    Carved in mystic runes upon the very living rock, the last words of deko
    of comp.lang.php make plain:
    [color=blue]
    > I'm trying to redirect requests for /index.php to
    > /mydirectory/index.php
    >
    > If I use an index file in / with only this line:
    >
    > <?php header("Locatio n:http://www.mysite.com/mydirectory/"); ?>
    >
    > that seems to work.[/color]

    That will work, so long as that's all you want to redirect. However, if
    someone visits http://www.mysite.com/mydirectory/someoldfile.php, it
    won't.
    [color=blue]
    > But can this be accomplished more efficiently with an htaccess
    > rewrite?
    >
    > I already have this rewrite rule in my htaccess file:
    > RewriteEngine on
    > Options All -Indexes
    > RewriteCond %{HTTP_HOST} ^www.myoldsite. com$ [NC,OR]
    > RewriteCond %{HTTP_HOST} ^myoldsite.com$ [NC]
    > RewriteRule ^(.*)$ http: //10.10.10.10/[/color]
    [color=blue]
    > This just sends requests for myoldsite.com into hyperspace, but allows
    > me to get email using that domain.[/color]

    ..htaccess has nothing to do with email, only web requests.
    [color=blue]
    > How do I append to htaccess so that I can redirect requests for a
    > specific host to a particular directory?[/color]

    RewriteCond %{HTTP_HOST} ^www.example.co m$
    RewriteCond %{REQUEST_URI} !^/example/
    RewriteRule (.*) /example/$1

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

    --
    Alan Little
    Phorm PHP Form Processor

    Comment

    • lorento

      #3
      Re: redirect to directory - php redirect or htaccess?

      This htaccess will redirect to particular directory from spesific host

      RewriteCond %{REMOTE_HOST} ^spesifichost.c om$
      RewriteRule ^(.*)$ http://www.yourhost.com/directory/

      hope it works
      --



      deko wrote:[color=blue]
      > I'm trying to redirect requests for /index.php to /mydirectory/index.php
      >
      > If I use an index file in / with only this line:
      >
      > <?php header("Locatio n:http://www.mysite.com/mydirectory/"); ?>
      >
      > that seems to work.
      >
      > But can this be accomplished more efficiently with an htaccess rewrite?
      >
      > I already have this rewrite rule in my htaccess file:
      > RewriteEngine on
      > Options All -Indexes
      > RewriteCond %{HTTP_HOST} ^www.myoldsite. com$ [NC,OR]
      > RewriteCond %{HTTP_HOST} ^myoldsite.com$ [NC]
      > RewriteRule ^(.*)$ http: //10.10.10.10/This just sends requests for
      > myoldsite.com into hyperspace, but allows me to get email using that domain.How
      > do I append to htaccess so that I can redirect requests for a specific host to a
      > particular directory?Thank s in advance.[/color]

      Comment

      • deko

        #4
        Re: redirect to directory - php redirect or htaccess?

        >> How do I append to htaccess so that I can redirect requests for a[color=blue][color=green]
        >> specific host to a particular directory?[/color]
        >
        > RewriteCond %{HTTP_HOST} ^www.example.co m$
        > RewriteCond %{REQUEST_URI} !^/example/
        > RewriteRule (.*) /example/$1
        >
        > RewriteCond %{HTTP_HOST} ^example.com$
        > RewriteCond %{REQUEST_URI} !^/example/
        > RewriteRule (.*) /example/$1[/color]

        Thanks - that helps.

        A couple of follow-up questions:

        Should I escape the periods in HTTP_HOST?

        RewriteCond %{HTTP_HOST} ^www\.example\. com$

        I assume that if I DO NOT escape them, then I would also match:

        wwwXexample.com

        or



        or both.

        Also, what do you think about the value of rewriting urls without the "www"?

        RewriteEngine on
        RewriteBase /

        RewriteCond %{HTTP_HOST} ^www\.example\. com$
        RewriteRule (.*) http://example.com/$1 [R=Permanent]


        Thanks for the help!


        Comment

        • Richard Levasseur

          #5
          Re: redirect to directory - php redirect or htaccess?


          deko wrote:[color=blue][color=green][color=darkred]
          > >> How do I append to htaccess so that I can redirect requests for a
          > >> specific host to a particular directory?[/color]
          > >
          > > RewriteCond %{HTTP_HOST} ^www.example.co m$
          > > RewriteCond %{REQUEST_URI} !^/example/
          > > RewriteRule (.*) /example/$1
          > >
          > > RewriteCond %{HTTP_HOST} ^example.com$
          > > RewriteCond %{REQUEST_URI} !^/example/
          > > RewriteRule (.*) /example/$1[/color]
          >
          > Thanks - that helps.
          >
          > A couple of follow-up questions:
          >
          > Should I escape the periods in HTTP_HOST?
          >
          > RewriteCond %{HTTP_HOST} ^www\.example\. com$
          >
          > I assume that if I DO NOT escape them, then I would also match:
          >
          > wwwXexample.com
          >
          > or
          >
          > www.exampleYcom
          >
          > or both.
          >[/color]

          Most likely not. The browser will look for the domain wwwXexample.com
          instead of example.com. the same is true for www.exampleYcom. The
          only thing that it could ever really match is a period because of how
          it has to be formatted.
          [color=blue]
          > Also, what do you think about the value of rewriting urls without the "www"?
          >
          > RewriteEngine on
          > RewriteBase /
          >
          > RewriteCond %{HTTP_HOST} ^www\.example\. com$
          > RewriteRule (.*) http://example.com/$1 [R=Permanent]
          >
          >[/color]

          Haha, did you read the conversatron by chance?

          Comment

          • deko

            #6
            Re: redirect to directory - php redirect or htaccess?

            > Most likely not. The browser will look for the domain wwwXexample.com[color=blue]
            > instead of example.com. the same is true for www.exampleYcom. The
            > only thing that it could ever really match is a period because of how
            > it has to be formatted.[/color]

            I see. I suppose it doesn't make much difference. But those who have a penchant
            for exacting code are likely to use the escape characters.

            In any case, I think I've got it working:

            RewriteEngine on
            Options All -Indexes
            RewriteCond %{HTTP_HOST} ^www\.deadsite\ .com$ [NC,OR]
            RewriteCond %{HTTP_HOST} ^deadsite\.com$ [NC]
            RewriteRule (.*) http://10.10.10.10/deadsite.com [L]
            #the above rule sends deadsite requests into hyperspace (there is a reason for
            this)
            RewriteCond %{HTTP_HOST} ^www\.site1\.or g$ [NC,OR]
            RewriteCond %{HTTP_HOST} ^site1\.org$ [NC]
            RewriteCond %{REQUEST_URI} !^/site1/.*$
            RewriteRule ^(.*)? /site1/$1 [L]
            #this rule catches any request for "www.site1. com" or "site1.com"
            #and sends it to the /site1 directory in public_html

            I have no idea what REQUEST_URI is doing or what the regex in the last
            RewriteRule is doing, but it works... so far, anyway. I am still testing.

            Another question:

            What does the [L] do and is it necessary? (the code seems to break if I don't
            use it)

            I will also try stacking up several domains (pointing to several subdirectories)
            this way. Is there any problem with having several conditions and rewrite
            rules? The file is just read sequentially, correct?



            Comment

            Working...