301 when www. infront of subdomain - apache

Could anyone please explain the redirect conditions and rule to achieve this:
User visits www.example.domain.com
301 to example.domain.com
(Basically removing the www. from the URL, but ONLY when they access a subdomain.
It must allow access at www.domain.com
Thanks!

Adding this to the htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) http://%1.domain.com/$1 [R=301,L]
will redirect www.example.domain.com to http://example.domain.com it will even redirect www.other.domain.com to http://oter.domain.com

Try this,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.([a-zA-Z0-9]+)\.domain\.com [NC]
RewriteRule ^(.*)$ http://%1.domain.com/$1 [L,R=301]

Related

how to 301 redirect from root domain to a page with htaccess

i am trying to auto 301 redirect from domain.com to domain.com/v2
ive tried this code
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^$ /v2/ [NC,L]
also tried
Redirect 301 / /v2/
i get the error 'site has tried to redirect you too many times'
is there any way to do this?
thanks for any help
Untested but this should redirect both domain.com and www.domain.com to the v2 subfolder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
You need to replace example.com with your actual domain because of posting restrictions.
I suppose you have an endless redirect because you are not matching the end of the domain string with $. Redirection should only take place at the root domain.
you can use a write rule with ^$ representing the root of the site and rewrite it to the selected folder
RedirectMatch ^/$ /v2/
it redirects the root and only the root URL.
or also you can use
RewriteEngine On
RewriteRule ^$ /v2[L]
If you want external redirection(301) you can use R flag
RewriteRule ^$ /v2[L,R=301]
Another way to use it with RewriteEngine
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
Original Answers
I hope I've helped

.htaccess redirect chain generated by multiple redirects

Wondering if you can help me?
I'm working on a site at the moment, and the following is in the .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule ^$ http://www.example/folder-name [R=301,L]
As I see it, this works in the following manner:
root domain rewrites using 301 to www. and then an additional 301 redirect to the /folder-name.
Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
NB. there is a subdomain, let's call that products.example.co.uk - and access would be needed here.
Is this possible? my .htaccess skills are not particularly tight so any help gratefully received!
This should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/folder-name/$1 [L,R=301]
Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
This should do it :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk [NC]
RewriteRule ^((?!folder).*)$ http://www.example.co.uk/folder/$1 [L,R=301]

.htaccess redirect subdomain dynamicly without changing url

I need to have a .htaccess redirect.
If I go to example.bla.org
I want it to redirect to bla.org/example
EXCEPT: It must keep the subdomain url can needs to be dynamic.
Ex: example.bla.org/apple -> bla.org/example/apple
I have tried almost every method like this one:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^examples.website.org [NC]
RewriteRule ^/(.*)$ /examples/$1 [L]
Please help, thanks!
You're pretty close, try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.website.org [NC]
RewriteRule ^(.*)$ http://website.org/example/$1 [L]
If you want to do a 301 redirect for SEO etc, add R=301 to the rewrite :
RewriteRule ^(.*)$ http://website.org/example/$1 [R=301,L]

Redirecting Subdirectory to Subdomain with either htaccess or cpanel

I want to redirect a subdirectory to a subdomain either using .htaccess or cpanel
redirects from
domain.in/subfolder
to
subfolder.domain.in
i m using cakephp app for both domain.in and subfolder.domain.in
each have separate core library
Please help me to solve this
Thanks in advance
You can use this rule as very first rule in your /subfolder/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.in$ [NC]
RewriteRule (.*) http://subfolder.domain.in/$1 [NE,R=302,L]
Or else:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.in)$ [NC]
RewriteRule (.*) http://subfolder.%1/$1 [NE,R=302,L]
Using the following .htaccess file might work:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.in [NC]
RewriteRule ^(.*)$ http://www.domain.in$1 [L,R=301]
RedirectMatch 301 ^/subfolder/(.*)$ http://subfolder.domain.in/$1

Redirect domain when there isn't a subdomain with htaccess

When someone types http://example.com I need it to be redirected to http://www.example.com but if someone goes to http://somenthing.example.com it should not be redirected.
I've been trying different rules with no luck, can anyone point to me the right htaccess rule to use here?
Try this
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Use this rule (place it on the top of your .htaccess .. as order of rules matters):
RewriteEngine On
# force www
RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteRule .* http://www.example.com%{REQUEST_URI} [L,R=301]