Mod_rewrite Subdomain Folder to Root Domain - .htaccess

I want to rewrite a folder on my Subdomain:
sub.domain.com/folder
to my main domain
domain.com/folder
I've tried this, but it doesn't work:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com/folder
RewriteRule ^(.*)$ https://domain\.com/folder/$1 [L]

You can use this :
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
RewriteCond %{REQUEST_URI} ^/folder
RewriteRule ^(.*)$ /$1 [L]

Related

Wildcard subdomain to subfolder in another subfolder

I want to redirect sudomains to subfolders with a .htaccess
Working:
subdomain.example.com -> example.com/subdomain ✓
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ %1/$1 [L,QSA]
Now I want to rewrite the subdomain in another subfolder:
Working, but with a redirect:
subdomain.example.com -> example.com/subfolder/subdomain ✓
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ https://example.com/subfolder/%1/$1 [L,QSA]
Not Working:
subdomain.example.com -> example.com/subfolder/subdomain x
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ subfolder/%1/$1 [L,QSA]
Why cant i add another subfolder? The subfolder is defenitely available (redirect working)
You may try this rule for subfolder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/subfolder/\1/ [NC]
RewriteRule ^(.*)$ subfolder/%1/$1 [L,QSA]

Redirecting to https and subfolder .htaccess

Situation: afwezigheden.eu is the main domain with a few coupled domains (= aliases?) like skatespots.be, inviteme.eu, halftwaalf.be... Now I'm trying to redirect those aliasses to a subfolder, without the users seeing it.
Al those sites are in a subfolder _DOMAINNAME. For example: skatespots.be is in afwezigheden.eu/_skatespots
Also I have an SSL certificate, so I would like to redirect all domains to https too.
I tried several things, the last one is this one, where skatespots seems to work and everything is redirected over https.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/_skatespots/
RewriteCond %{HTTP_HOST} ^(www\.)?skatespots\.
RewriteRule ^(.*)$ /_skatespots/$1 [L]
RewriteCond %{REQUEST_URI} !^/_halftwaalf/
RewriteCond %{HTTP_HOST} ^(www\.)?halftwaalf\.
RewriteRule ^(.*)$ /_halftwaalf/$1 [L]
RewriteCond %{REQUEST_URI} !^/_inviteme/
RewriteCond %{HTTP_HOST} ^(www\.)?inviteme\.
RewriteRule ^(.*)$ /_inviteme/$1 [L]
RewriteCond %{HTTP:X-Forwarded-Proto} !http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What am i doing wrong?
What if you put the folder structure as this:
afwezigheden.eu/sites/skatespots.eu
afwezigheden.eu/sites/halftwaalf.eu
And then use this in your afwezigheden.eu/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !sites/
RewriteRule (.*) /sites/%{HTTP_HOST}/$1 [L]

Htaccess redirect from subdomain, but not it's folders

I have
sub.domain.com and want it to be redirected to domain.com
But there must not be redirects from e.g. sub.domain.com/folder/ or sub.domain.com/folder/index.html
Everything i've tried does not work...
For example
RewriteEngine on
RewriteCond %{HTTP_HOST} sub.domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.domain.com/ [L,R=301]
You can use this :
RewriteEngine on
RewriteCond %{HTTP_HOST} sub.domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/folder/?$
RewriteCond %{REQUEST_URI} !^/folder/index\.html$
Rewriterule ^(.*)$ http://www.domain.com/ [L,R=301]

Hide Subfolder in URL with .htaccess without breaking other subdomains

I am trying to keep my main domain structure from being too cluttered so I am parsing all of my domains into their own subfolder. So, what I am trying to do is when a user goes to http://mydomain.com they are actually sent to http://mydomain.com/sub-directory
This bit of code works:
#redirect to submain subdomain
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /submain/
RewriteRule ^submain/(.*) /$1 [L,R=301]
RewriteRule !^submain/ submain%{REQUEST_URI} [L]
However it breaks all the other subdomains I have loaded into my main directory.
Any ideas on how to fix this?
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /submain/
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteRule ^submain/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteRule !^submain/ submain%{REQUEST_URI} [L]
Create a .htaccess file in root folder, and put this content inside(just change example.com and my_subdir):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L]
</IfModule>

redirect a domain to a certain folder (Joomla Site)

I have a website hosted in cpanel. Files of this website are in a folder. In the panel there is no possibility to host a domain to this folder, so when I go to www.mydomain.com I can see all folders. I would like to redirect my registered domain to a folder where my website is.
I use htaccess file and try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule ^(.*)$ www.hostdomain.com/folder/$1 [L]
but it doesn't work.
This:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule ^(.*)$ folder/$1 [L]
and still nothing.
What I do wrong? I would be grateful for some advice.
Edit: I dealt with it.
I use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/folder/
RewriteRule ^(.*)$ /folder/$1 [L]
then in order to hide joomla folder I add to joomla htaccess:
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) http://www.mydomain.com/$1/ [R=301,L]
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule ^(.*)$ folder/$1 [L]
and next I change configuration.php file in joomla folder:
var $live_site = 'http://www.mydomain.com/';
It works.
Thanks a lot for your answers.
test it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteCond %{REQUEST_URI} !^/path/to/folder/
RewriteRule ^(.*)$ path/to/folder/$1 [L]

Resources