Google Answers Logo
View Question
 
Q: Stoping a .htacess file from applying to sub-directories? ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Stoping a .htacess file from applying to sub-directories?
Category: Computers > Programming
Asked by: andrew_g-ga
List Price: $20.00
Posted: 13 Aug 2005 15:33 PDT
Expires: 12 Sep 2005 15:33 PDT
Question ID: 555440
How do folks?

I have three Websites hosted by my ISP each each stored in it's own
subdirectory off the root. I recently had to add an htaccess file to
this root directory to redirect users from an old website address to a
new domain I had purchased (again hosted in a sub-directory).

However this htaccess file is now applying site-wide and across all
aliases meaning all three of my sites now point to just one site

As I understand htaccess files cascade downwards, so that if I have a
.htaccess file in my root directory, /,
and another in a sub-directory called /Wright, then when a file is
requested from the /Wright directory Apache will merge the two files
and use all the directives.

Is there any way I can stop this behaviour - I have a 301 redirect in
my root directory and I don?t want it to apply to the /Wright
sub-directory. I have tried unsuccessfully putting a 301 redirect in
the /Wright sub-directory to override the root but this has not been
successful.

I tried another .htaccess file in one of the sub-directories housing
one of the affected sites, but all that happened when I tried visiting
the affected url was the browser hung. It kept trying to open the page
but eventually timed out.

Also (just to complicate things) I should add that the document root
of my old Website and the overall root were the same.

Thanks in hopeful anticipation folks

Request for Question Clarification by wildeeo-ga on 15 Aug 2005 17:32 PDT
Hi,

The easiest way of achieving this is probably to create a php script
that redirects the user to your new site, rather than doing it with a
.htaccess file.

Is this an acceptable solution?

--wildeeo

Clarification of Question by andrew_g-ga on 16 Aug 2005 04:51 PDT
Hi

I've done a bit of further digging and it seems that the problem is
that mod_alias' Redirect directive is unconditional....whatever that
means :-)

I'm told I should be using a mod_rewrite instead but I haven't a clue
how to write/configure this.

My Host is actually an ISP so as I understand it I'm limited on the
type of scripts that be run on the Server. I've a feeling PHP is a no
no because my ISP has a specific server for this (hosting of forums
etc) and it's not the one my Site files are on :-(

Request for Question Clarification by wildeeo-ga on 17 Aug 2005 11:40 PDT
Hi,

It definitely can be done with mod_rewrite, but not all hosts have it enabled.

You can check if your host has it enabled by creating a .htaccess file
in some directory, and putting the following in the file:

Options +FollowSymLinks
RewriteEngine On

Then, try browsing to that directory. If you get an error (usually a
'500 Internal Server Error'), it's not enabled. If you don't see an
error, you can use mod_rewrite.

Let me know what happens. If it is enabled, I can use it to give you a
.htaccess file to redirect your visitors.

Clarification of Question by andrew_g-ga on 17 Aug 2005 14:17 PDT
wildeeo-ga 

Many thanks for your reply.
I did as you suggested and got a Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator, webmaster@plus.net and inform
them of the time the error occurred, and anything you might have done
that may have caused the error.
More information about this error may be available in the server error log.

Is there anything else I can do given that it seems a simple 301
redirect will not do the trick?

Cheers

mod_alias' Redirect directive is unconditional.
Answer  
Subject: Re: Stoping a .htacess file from applying to sub-directories?
Answered By: wildeeo-ga on 17 Aug 2005 19:35 PDT
Rated:5 out of 5 stars
 
Hi,

After poking around the Apache documentation, I've come across a
directive, 'RewriteMatch', which should let you achieve what you want.

If you put this in a .htaccess file in your document root (/):

RedirectMatch      301      ^/([^/]*)$      http://some.site/$1

...it will redirect any address that isn't a subdirectory to some
other address using a 301 redirect. So, for example, here are a few
URLs and the address you'd end up at:

http://old.site/  ->  http://some.site/
http://old.site/page.html  ->  http://some.site/page.html
http://old.site/subdirectory/  ->  http://old.site/subdirectory/
http://old.site/subdirectory/page.html  ->  http://old.site/subdirectory/page.html
http://old.site/subdirectory  ->  http://some.site/subdirectory

If this isn't what you wanted or it doesn't work properly, feel free
to request a clarification.

--wildeeo

Request for Answer Clarification by andrew_g-ga on 19 Aug 2005 14:28 PDT
Thanks for this wildeeo

If a RedirectMatch works on my ISP's Servers (any way of testing
this?)then it's EXACTLY what I'm looking for.

My overall root directory is /htdocs 

However this was also the site root for my old Website namely
http://www.danny.example.co.uk

I have now moved this site?s contents to a new sub directory called
danny i.e. /htdocs/danny and my new domain of
http://www.tree.example.co.uk points to this.

I have a .htacess file with the contents below successfully
redirecting from http://www.danny.example.co.uk to
http://www.tree.example.co.uk

redirect 301 /index.htm http://www.tree.example.co.uk/ 

My problem is that two further websites of mine each hosted in their
own sub-directory i.e. /htdocs/right and /htdocs/nick have both
started redirecting to http://www.tree.example.co.uk since the working
.htacess file on www.tree.example.co.uk was implemented.

Would it please be possible to knock up a ReWriteMatch directive based
on the above?

Cheers

Clarification of Answer by wildeeo-ga on 19 Aug 2005 16:24 PDT
Hmm. If you're using subdomains, that means the RewriteRule won't
work; it can only match the path of the file, but we need to match the
subdomain.

This also means that there's no easy way to use a .htaccess file to
redirect your visitors. mod_rewrite is the only way this can be done.

In the mod_rewrite test above, it might be your host just doesn't like
the 'Options +FollowSymLinks' line... it might be worth creating the
file again, but this time without the 'Options +FollowSymLinks' line.
If you don't get an error this time, we can use a .htaccess file.

If you still get the error, the only way is to create a special index
file to redirect visitors.

The best way of doing this is using a PHP script (as I mentioned
above) since it allows you to use a 301 redirect and will be basically
transparent to the visitor.

To see if your host allows php scripts, create and upload a file
called test.php and put the following in it:

<? header('Location: http://google.com/'); ?>

Then try visiting it in your browser. If it redirects you to google,
we have liftoff.

If this doesn't work either (you'll probably just see the line of
code), then there is a way of redirecting people using a html file,
but this isn't a 'proper' redirect, and search engines may not show
your site properly.

Let me know how it goes.

Request for Answer Clarification by andrew_g-ga on 20 Aug 2005 15:20 PDT
wildeeo-ga

I created a new .htaccess file without the 'Options +FollowSymLinks' line i.e. just
RewriteEngine On
and when I visited the old url (which I want to redirect from) this
time I get an Index of /  - list of all sub-directories on my site.

Again I created the test .php file as you recommended but I was not
re-directed to Google as you had hoped. Again all I got was a an Index
of /  - list of all sub-directories on my site.
:-(

Clarification of Answer by wildeeo-ga on 22 Aug 2005 13:27 PDT
Hi,

The fact you got the directory listing is a Good Thing - we should be
able to use mod_rewrite to do the magic.

Using the examples above, the .htaccess file below *should* work to
redirect visitors from www.danny.example.co.uk to
www.tree.example.co.uk.

RewriteEngine On
RewriteCond %{HTTP_HOST} danny\.example\.co\.uk$
RewriteRule (.*) http://www.tree.example.co.uk/$1 [R=301]

This will redirect ANY request for anything at www.danny.example.co.uk
to the same file on www.tree.example.co.uk.

Let me know how it goes.

--wildeeo

Request for Answer Clarification by andrew_g-ga on 22 Aug 2005 14:46 PDT
wildeeo-ga

Good to hear from you as always, I appreciate your continued help.
It Works!!! :-) :-)

Now Part Two - can we get visitors to two further websites of mine
each hosted in their own sub-directory of the above root i.e.
/htdocs/wright and /htdocs/nicky to redirect to
http://www.sidexample.co.uk and http://www.nickyexample.co.uk
respectively instead of http://www.tree.example.co.uk as current.

Regards

Clarification of Answer by wildeeo-ga on 22 Aug 2005 15:26 PDT
Try this:


RewriteEngine On

RewriteCond %{HTTP_HOST} danny\.example\.co\.uk$ [NC]
RewriteRule ^wright(.*) http://www.sidexample.co.uk$1 [R=301,NC,L]

RewriteCond %{HTTP_HOST} danny\.example\.co\.uk$ [NC]
RewriteRule ^nicky(.*) http://www.nickyexample.co.uk$1 [R=301,NC,L]

RewriteCond %{HTTP_HOST} danny\.example\.co\.uk$ [NC]
RewriteRule (.*) http://www.tree.example.co.uk/$1 [R=301,NC,L]


--wildeeo

Request for Answer Clarification by andrew_g-ga on 23 Aug 2005 15:19 PDT
wildeeo-ga 

You Sir are a genius!! :-)
It works - many, many thanks.
You would not believe how many Forums and Technical Groups I posted
this query with to no avail.
Much appreciated.
Best Regards
AG

Clarification of Answer by wildeeo-ga on 23 Aug 2005 15:24 PDT
Glad it works. :-)
andrew_g-ga rated this answer:5 out of 5 stars and gave an additional tip of: $10.00
Genius! Much appreciated. I couldn't be happier!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy