cancel
Showing results for 
Search instead for 
Did you mean: 

htaccess redirect to subdir

gandb
Dabbler
Posts: 14
Registered: ‎01-09-2009

htaccess redirect to subdir

I'm trying to redirect all requests to www.gtsagedev.co.uk to a subdir "/public/gtsagedev"
In /public/.htaccess I have
RewriteEngine on
# www.gtsagedev.co.uk to point to gtsagedev dir
RewriteCond %{HTTP_HOST} ^www.gtsagedev.co.uk$ [nc]
RewriteRule (.*) /gtsagedev/$1

but I'm getting 500 Internal server error
When I comment out these lines, www.gtsagedev.co.uk/gtsagedev/index.php works fine.
What have I got wrong?
20 REPLIES 20
bobpullen
Community Gaffer
Community Gaffer
Posts: 16,887
Thanks: 4,979
Fixes: 316
Registered: ‎04-04-2007

Re: htaccess redirect to subdir

Try this:
RewriteEngine on
RewriteCond %{HTTP:Host} ^(?:www.gtsagedev.co.uk)?$
RewriteCond %{REQUEST_URI} !^/gtsagedev/
RewriteRule ^(.*) /gtsagedev/$1 [NC,L,NS]

I make no claims to be a mod rewrite expert and I'm not 100% on what each of the above expressions is doing but it seems to work for me. If any part of it's unnecessary or not quite right then I'm sure somebody will be along to correct me shortly Wink

Bob Pullen
Plusnet Product Team
If I've been helpful then please give thanks ⤵

spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: htaccess redirect to subdir

The original .htaccess will be looping and the %(REQUEST_URI) condition prevents that.
Strictly backslashes are needed in the host string to make the full stops into real ones.
This is similar to Bob's (but using the correct HTTP_HOST) and should work *
RewriteEngine on
# Point domain to /gtsagedev folder
RewriteCond %{HTTP_HOST} ^www\.gtsagedev\.co\.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/gtsagedev/
RewriteRule (.*) gtsagedev/$1

Edit: but apparently doesn't - see reply #7 Sad
David
bobpullen
Community Gaffer
Community Gaffer
Posts: 16,887
Thanks: 4,979
Fixes: 316
Registered: ‎04-04-2007

Re: htaccess redirect to subdir

@spraxyt, Is there something in your code that would cause problems with subdomains other than www? My directives are for non-www subdomains and your method doesn't work for me. It doesn't error, however the contents of /public are displayed rather than the redirection folder Huh
Apologies for the naivety but this really isn't my forté Wink

Bob Pullen
Plusnet Product Team
If I've been helpful then please give thanks ⤵

spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: htaccess redirect to subdir

Bob, my rule is designed only for a www subdomain and wouldn't apply for anything else. That is what the OP used in the opening post.
I *think* yours applies for 0 or 1 occurrence of the domain string, in effect anything though I'm not entirely sure of this.
I use the style of my post for a domain in my own .htaccess though checking back that also has RewriteBase /
Within the voodoo of mod_rewrite I'm stuck on whether that is significant at the moment.
David
David
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: htaccess redirect to subdir

Alternatively:
RewriteCond %{HTTP_HOST} ^([^.]+\.)?yourdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond %{REQUEST_URI} !^/yoursite/
RewriteRule (.*) /yoursite/$1

Link.
Don't need to turn on the rewrite engine again.
Gabe
bobpullen
Community Gaffer
Community Gaffer
Posts: 16,887
Thanks: 4,979
Fixes: 316
Registered: ‎04-04-2007

Re: htaccess redirect to subdir

Thanks David.
I'll stick to my own directives at the moment as they're doing what I want them to do. I don't think I can wildcard the subdomain because I've different subdomains under the same domain pointing to different folders.

Bob Pullen
Plusnet Product Team
If I've been helpful then please give thanks ⤵

gandb
Dabbler
Posts: 14
Registered: ‎01-09-2009

Re: htaccess redirect to subdir

Thanks to all - Bob's code in reply#1 works fine - with or HTTP:Host or HTTP_HOST - but (surprisingly) spraxyt's doesn't.
Now all I need to do is to remove the spurious gtsagedev in the address url's (eg. http://www.gtsagedev.co.uk/gtsagedev/index.php/contact-me), but I vaguely remember how to do that - will update when I have!
Update: It's in the Joomla configuration.php live_site$ variable.  (Yes, I'm running Joomla...)
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: htaccess redirect to subdir

I think the unwanted /gtsagedev in the URLs can be eliminated by removing it from all the links on the website pages.
David
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: htaccess redirect to subdir

Quote from: spraxyt
RewriteRule (.*) gtsagedev/$1 


needs to be
RewriteRule (.*) /gtsagedev/$1 

when in .htaccess. In conf it's without the leading slash.
Gabe
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: htaccess redirect to subdir

Thanks, I think having RewriteBase / in my .htaccess (for other reasons) makes my relative path work. Huh
David
gandb
Dabbler
Posts: 14
Registered: ‎01-09-2009

Re: htaccess redirect to subdir

Isn't mod_rewrite fun? Have reread the documentation for the 1st time in about 5 years and it's making some sort of sense again...  I found AddedBytes intro pretty useful.
Anyway, it's all working as I want now, so thanks again.
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: htaccess redirect to subdir

Quote from: spraxyt
I think having RewriteBase / in my .htaccess (for other reasons) makes my relative path work. Huh

Yup, that'll do it. If you have a leading slash though, it'll work with or without the rewritebase. Apart from that, the main difference between your recipe and Bob's is that Bob's condition matches either the stated host or an empty host header (not just anything), which could be useful if a request without a host header could ever reach your rewrite, but in this context it can't, so I'm not sure it is.
Gabe
pedroc
Dabbler
Posts: 18
Thanks: 1
Fixes: 1
Registered: ‎23-03-2008

Re: htaccess redirect to subdir

In simple terms, I need to redirect traffic into my site to a new subdirectory.  I am struggling, so to explain -
Due to a change in file structure I now need to redirect all historical links for my site to a subdirectory called 'catalog', but I am having the same problem as the first poster.
I cannot seem to resolve it with any of the suggestions above.
I have built a  .php website on the Hostopia server This is linked as a sub-domain of my main html site.  This cgi site is to replace an earlier site that didn't use the 'catalog' directory, so now anybody trying an old link gets a 'page not found' message.
So I want all  links to the old site to be redirected to the same path as before, but within the 'catalog' directory. 
I can redirect specific files eg Index.php, but can't find the code to redirect all traffic for the other web pages in the same way.
I just can't get it to work - I must be missing something, can anybody advise please?
Pedro C
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: htaccess redirect to subdir

I would have expected adapting the examples given earlier to have provided what you describe. Since they don't it suggests the requirement is more complex.
Could you post a .htaccess file example that works for an index.php file? Forum members would then be better placed to advise on generalising the rule.
David
David