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. :-)
|