2

I've searched everywhere and used some examples but they don't meet my specific needs, hence why I'm asking here if anyone can please help? I know the http:: below isn't correct, it's just because I can't post links here.

I'd like to redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/ whilst also being able to redirect

  1. http:://www.mysite.co.uk/mysub/ and 2. http:://mysite.co.uk/mysub/ to 3. http:://mysub.mysite.co.uk/

All files in 1. and 2. should be redirected to their equivelant in 3.

For example: http:://www.mysite.co.uk/mysub/file.html AND http:://mysite.co.uk/mysub/file.html should both go to http:://mysub.mysite.co.uk/file.html

This is to affect the subdomain only, so that other folders and files in the site root aren't affected.

If anyone could help me understand and write the code for the 301 redirect in a .htaccess file I'd really really appreciate it! Thanks!

P3tro
  • 139
  • 1
  • 3
  • 9

1 Answers1

0

This is the code you'll need in your .htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /

# To redirect http:://www.mysite.co.uk/ to http:://mysite.co.uk/
RewriteCond %{HTTP_HOST} ^www\.(mysite\.co\.uk)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# To redirect /mysub/foo to http://mysub.mysite.co.uk/foo
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.co\.uk)$ [NC]
RewriteRule ^(mysub)/?(.*)$ http://$1.%2/$2 [R=301,L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks, that helps, but there's just one thing that isn't working: http:://www.mysite.co.uk/mysub is not going to http:://mysub.mysite.co.uk/ – P3tro Feb 03 '12 at 12:19
  • Hi, http:://www.mysite.co.uk/mysub now just takes you to http:://mysite.co.uk/ and not the subdomain as it's supposed to? – P3tro Feb 03 '12 at 14:07
  • Oh Sorry, I think I made a mistake by not making / optional. Edited my answer one more time, it should work now. Pls check. – anubhava Feb 03 '12 at 14:20
  • Hi @anubhava, sorry for the delayed response. http:://www.mysite.co.uk/mysub does **not** redirect to http:://mysub.mysite.co.uk Everything else is fine. – P3tro Feb 06 '12 at 10:15
  • Made some minor updates, pls try it again. However it will help if you can post your complete .htaccess in your question. – anubhava Feb 06 '12 at 10:45
  • That's not fixed it either I'm afraid. There is nothing in the .htaccess file except your code, with my edits for the domain name only. Thanks for your persistance! – P3tro Feb 06 '12 at 11:16
  • The thing is everything I have tested on my end and it is all working fine on my Apache (Mac and Linux). Can you clear your browser cache and try again? Is `http:://www.mysite.co.uk/mysub/` working fine? If nothing works pls post matching lines of your access.log and error.log here. – anubhava Feb 06 '12 at 11:25
  • It's just http:://www.mysite.co.uk/mysub/ that doesn't work (with or without the trailing slash). Everything else works fine. I've tried with IE and FF and cleared both caches. I'll just see what I can get from any logs... – P3tro Feb 06 '12 at 11:29
  • mysite.co.uk 78.***.***.3 - - [06/Feb/2012:11:31:35 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)" The above is the access log. The only error log entry is for missing favico files. Do you think I should ask for support from the web host provider as it could be their setup? – P3tro Feb 06 '12 at 11:34
  • That's strange since at least it should have a 404 line for `http:://www.mysite.co.uk/mysub/`. btw is `http:://mysite.co.uk/mysub/` working? – anubhava Feb 06 '12 at 11:39
  • Yeah http:://mysite.co.uk/mysub/ works fine (it redirects as it should). If it were to generate a 404 error would I not see a apache 404 page? At the moment it's just taking me to http:://mysite.co.uk/ – P3tro Feb 06 '12 at 11:57
  • could you please show me the previous version of the code as that would have been the next best thing? – P3tro Feb 07 '12 at 09:23
  • Sure edited again to revert the latest changes and go back to previous version. – anubhava Feb 07 '12 at 09:56
  • I dont know if this is related but anything like www.mysite.co.uk/folder just reverts to mysite.co.uk ? – P3tro Feb 07 '12 at 10:28
  • Ah I think that is indeed the problem. Can you please try out the latest code ( I just edited ). – anubhava Feb 07 '12 at 10:36
  • You're welcome and thanks to you for providing me all the helpful info to resolve this. – anubhava Feb 07 '12 at 13:15