Home > Archive > PHP Programming > June 2006 > redirect to directory - php redirect or htaccess?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
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("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 2006-06-26, 7:58 am |
| Carved in mystic runes upon the very living rock, the last words of deko
of comp.lang.php make plain:
> 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.
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.
> 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.
..htaccess has nothing to do with email, only web requests.
> How do I append to htaccess so that I can redirect requests for a
> specific host to a particular directory?
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 2006-06-26, 7:58 am |
| 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:
> 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.
| |
|
| >> How do I append to htaccess so that I can redirect requests for a
>
> 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
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 2006-06-27, 6:58 pm |
|
deko wrote:
>
> 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.
>
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.
> 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]
>
>
Haha, did you read the conversatron by chance?
| |
|
| > 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.
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?
|
|
|
|
|