By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,334 Members | 2,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,334 IT Pros & Developers. It's quick & easy.

redirect to directory - php redirect or htaccess?

P: n/a
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.

Jun 25 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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/
Jun 26 '06 #2

P: n/a
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.


Jun 26 '06 #3

P: n/a
>> 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


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!
Jun 27 '06 #4

P: n/a

deko wrote:
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


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?

Jun 27 '06 #5

P: n/a
> 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?

Jun 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.