klados
klados

Reputation: 787

.htaccess redirect to another folder but keep the original domain

First,

my 'example.com' domain is linked to '/home/defaultfolder/' folder.

I want to redirect

example.com , www.example.com

to

/home/somefolder/example/ , not /home/defaultfolder/ folder

by using .htaccess. (both are inside the DocumentRoot directory)

also, subdomains like

a.example.com

to

/somefolder/example/a/

but keeps the 'example.com' domain.

I have tried some examples on the web, but nothing could have done it.

How can I write the .htaccess to do so? Thank you.

Of course, I cannot change server settings(like alias virtual hosts..) and that's why I am trying to do it by modifying the file.


I have tried

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule /somefolder/example/$1 [R=301,NC,L]

RewriteCond %{HTTP_HOST} ^(www.)?example.com$   
RewriteRule !^/somefolder/example/ /somefolder/example/1%{REQUEST_URI} [L]


RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example.com
RewriteRule ^(.*)$ /somefolder/example/$1 [R=permanent,L]

and some others..

Upvotes: 1

Views: 334

Answers (2)

anubhava
anubhava

Reputation: 786091

Have it this way in /home/.htaccess (site root):

RewriteEngine On

# handle example.com
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example)\.com$ [NC]
RewriteRule .* somefolder/%1/$0 [L]

# handle any sub.example.com
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.(example)\.com$ [NC]
RewriteRule .* somefolder/%2/%1/$0 [L]

Upvotes: 1

Samson
Samson

Reputation: 187

I think you should modify the server setting, make the domain root folder to what you want.

Upvotes: 0

Related Questions