Connecting Tech Pros Worldwide Forums | Help | Site Map

redirect to directory - php redirect or htaccess?

deko
Guest
 
Posts: n/a
#1: Jun 26 '06
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("Location: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?Thanks in advance.


Alan Little
Guest
 
Posts: n/a
#2: Jun 26 '06

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("Location: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.com$
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
http://www.phorm.com/
lorento
Guest
 
Posts: n/a
#3: Jun 26 '06

re: redirect to directory - php redirect or htaccess?


This htaccess will redirect to particular directory from spesific host

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

hope it works
--
http://blog.deshot.com
http://www.groupvita.com

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("Location: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?Thanks in advance.[/color]

deko
Guest
 
Posts: n/a
#4: Jun 27 '06

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.com$
> 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.

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!


Richard Levasseur
Guest
 
Posts: n/a
#5: Jun 27 '06

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.com$
> > 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?

deko
Guest
 
Posts: n/a
#6: Jun 28 '06

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\.org$ [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?



Closed Thread