Forum Moderators: phranque

Message Too Old, No Replies

.htaccess : cam I redirect URL to subdirectory?

.htaccess redirect URL

         

nheaps

1:04 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



Hi,

I'm new here, hope someone would kindly help me with my first query!

Using a .htaccess file, I need to find a way to redirect an incoming URL of "http://www.blah.com/" to a subdirectory "/subdir" under the document root on my server, so that when someone types the address into their browser, the files will be looked for in the subdirectory and not the root directory. Furthermore, it must apply only to "http://www.blah.com/", as I host multiple web sites on my server, and they mustn't be affected.

The reason I need to do this is because my hosting package does not allow me to restart httpd, and new entries in the httpd.conf file to effect the above have not come into play yet, and I can't wait around for the next automatic restart.

Many thanks!

Nick

nheaps

1:42 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



Well, I seem to have concocted a solution which works -- here's what I've done:-

RewriteEngine on
RewriteCond %{HTTP_HOST} blah\.com [NC]
RewriteRule (.*) [myDocumentRoot.myWebHost.com...] [R=301]

...although I'm not too sure what the [NC] means, if anyone would be kind enough to enlighten me?

Any comments gratefully appreciated.

Many thanks,

Nick

Wizcrafts

2:02 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



...although I'm not too sure what the [NC] means, if anyone would be kind enough to enlighten me?

Nick;

Welcome to WebmasterWorld!

The [NC] simply means NoCase, case insensitive spelling. Any match of upper or lower case letters will be accepted.

Wiz

nheaps

2:46 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



Thanks Wiz.

The thing I'd like to do now that it's working is stop the URL changing in the address bar. I'm typing in [blah.com,...] and it's changing to [myWebSpace.myWebHost.co.uk...] which looks very ugly indeed!

I've tried changing the Rewrite rule as follows:-

RewriteRule (.*) /subdir$1 [R]

...but the web server gets into a nasty loop, leaving multiple instances of the subdir name in the address bar.

Any ideas gratefully accepted.

Many thanks,

Nick

jdMorgan

2:57 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nick,

Welcome to WebmasterWorld [webmasterworld.com]!

A couple of questions: First, do you really want or need a 301 redirect, or would a simple rewrite be better? The reason I ask this is that using a 301 "exposes" the fact that you are mapping to a subdirectory to users and search engines alike, whereas a rewrite simply "steers" the requests to the subdirectory silently.

Secondly, do you already have the httpd.conf code in place, awaiting the restart? If so, you may wish to exclude requests for "/subdir" from being rewritten in .htaccess using a RewriteCond. That way, when the server is restarted, you won't have a looping problem as both sets of code interefere with each other. (This applies only to the rewrite solution, where the hostname is not changed.)


RewriteEngine on
RewriteCond %{HTTP_HOST} blah\.com [NC]
RewriteCond %{REQUEST_URI} !/subdir
RewriteRule (.*) /subdir/$1 [L]

Jim

nheaps

6:07 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



Jim,

Yes, all I want is a 'silent' redirect -- I'd included the 301 redirect because I was copying some code I'd found elsewhere on the forum, and I wasn't 100% sure what all the bits did. I have a much better idea now.

I do indeed have the httpd.conf code in place, and am grateful for your excellent suggestion of excluding requests for "/subdir" from being rewritten. Many thanks for that, and the snippet of code you suggested, which seems to work a treat.

Of course, if I were able to somehow restart Apache it would remove the need for all of this -- I don't suppose there's any way of 'inducing' a restart through some sort of back-door, is there? (I don't have root access, nor do I have access to the main httpd process! So I can't do "kill -USR1 <pid>" or "httpd stop" / "httpd start".)

Thanks again.

Nick

jdMorgan

7:48 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> any way of 'inducing' a restart through some sort of back-door, is there?

No, I certainly hope not! What a troublemaker's dream that would be! :o

Actually, there are probably several ways to do it, but none that would not also have the side effect of making your host close down your account.

Jim