473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

Jun 25 '06 #1
5 4976
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
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
>> 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

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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JW | last post by:
I have a directory protected with .htaccess / .htpasswd. After I'm validated, I run a php script which bombs out when trying to write a file to that directory. If I chmod 777 on the directory...
3
by: Sean Berry | last post by:
Hi there. I am relativly new to Python CGI and need a question answered. I have made custom 404 error pages and have them in various web directories. I have not been able to figure out a way...
4
by: TomT | last post by:
Hi.. I'm redirecting users to another page using: response.redirect("newpage.asp") this works... But I need to add a variable to the page specified.. IE: newpage.asp?id=JobID
1
by: Peter Kirk | last post by:
Hi there I have a program written by another company (it's a "web control" which returns a web-page: can I compare this to a servlet in the Java world?), which they think is causing problems on...
6
by: DotNetGruven | last post by:
I've got a page that does a search... There is an ImageButton on it that allows the user to reset the search. The Click Handler for the button clears out the Data and then redirects to the same...
3
by: Rik G. | last post by:
I'm trying to replace a PHP query string with virtual directories using Apache's mod_rewrite. Here's my test .htaccess: RewriteEngine on RewriteRule ^qqq$ database.php?cat=0 RewriteRule...
1
by: nickyeng | last post by:
I have checked this info from apache website, and i confused with it. Procteing System files it said: To run a really tight ship, you'll want to stop users from setting up .htaccess files...
7
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I am trying to set this up using asp code and IIS configuration. But it seems not working. Here it is the way I am doing. In IIS I set up a virtual directory with secure communication, I...
2
by: RuthC | last post by:
Hi. I am using .htaccess to redirect in my website Now I want to redirect a url, which Contain '-' to another url. ex: RewriteRule ^book-and-magazine-discount/ query.php?q=$1.
1
by: Alvis | last post by:
Hey, so I'm trying to figure out how I can redirect all non swedish users to the /en/ directory using htaccess This is what I have now: #For every other language (including English :)) use...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.