Question

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!

Was it helpful?

Solution

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top