HTACCESS redirect to folder based on hostname - .htaccess

I have multiple websites under the same web server.
My root structure is as following:
./
./site1/
./site2/
./.htaccess
I would like htaccess file to redirect to the correct folder.
If the user navigates to www.site1.com or site1.com it should rewrite the url to go to the ./site1/ folder and if the user navigates to www.site2.com or site2.com it should rewrite the url to go to the ./site2/ folder.
Is there any way to achieve this behaviour?
Thanks in advance.

Use following code in .htaccess file.
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com$ [NC]
RewriteRule !^site1/ /site1%{REQUEST_URI} [L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?site2\.com$ [NC]
RewriteRule !^site2/ /site2%{REQUEST_URI} [L,NC]

Related

I changed my root document folder to use a subdirectory but its not working

what I want to achieve is my domain.com will use the subdirectory /new as the root document folder but when I tried visiting the site it redirects me to domain.com/new which is not what I want
the script was working before, but after i edit the script to use a different folder it doesn't work as expected.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !new/
RewriteRule (.*) /new/$1 [L]
it redirects me to domain.com/new. i dont want /new to be shown in the url. thank you in advance!
Change your rules with this rewrite
RewriteEngine on
RewriteBase /new/

htaccess for subdomain no longer in use

I had a subdomain https://sub.rootdir.com/ which was migrated to the root domain and to a new host.
I want to redirect this subdomain to the root folder. The host told me to re-create the subdomain and then create an '.htaccess' file in which you can set redirection rules such as:
Redirect 301 / https://rootdir.com/
I have kind of no idea what to type in the htaccess file.
Can I please get help from you?
Create a .htaccess file in your subdomain folder and enter this code in;
RewriteEngine On
RewriteCond %{HTTP_HOST} !^rootdir\.com
RewriteRule (.*) https://rootdir.com/$1 [R=301,L]
I figured it out from other posts here tx
#mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.rootdir.com$ [NC]
RewriteRule ^(.*)$ https://www.rootdir.com%{REQUEST_URI} [R=301,NC,L,QSA]

Automatically redirect subdomain to folder path?

Is it possible to automatically redirect subdomains to a folder (structure)? I want to change the setup of my site from subdomain to folder and there a lot of redirects to be done, so i was wondering if there is any automatic solution.
What i want is i.e.:
subfolder1.domain.com
to be redirected to (internal redirect, same top-level domain) www.domain.com/folder/subfolder1
Where 'subfolder1' is relative (not static).
So is there a general htaccess code i can use?
Thanks
You can put this code in your htaccess (which has to be in document root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.(domain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%2/folder/%1/$1 [R=301,L]
Note: by redirect in your question, i understood it as an external redirect. If that's not the case, i'll update my answer
EDIT (internal rewrite)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.domain\.com$ [NC]
RewriteRule ^((?!folder/).*)$ /folder/%1/$1 [L]

Redirect users with .htaccess file

I want to redirect users to another domain name when they visit a certain directory in my website. For example; if the user visits /files then it would redirect them to www.myfiles.com. However, I would also like to do the same with subdomains so that when the user goes to /files or files.domain.com they are redirected to www.myfiles.com, for example.
How can I do this in a .htaccess file?
I hope people can understand what I am trying to describe.
use this
Redirect 301 /files http://www.myfiles.com
Try adding the following to the .htaccess file in the root directory of your domain.com site.
RewriteEngine on
RewriteBase /
#if either files.domain.com
RewriteCond %{HTTP_HOST} ^files\.domain\.com$ [NC,OR]
#or /files
RewriteCond %{REQUEST_URI} ^/files$ [NC]
#redirect to www.myfiles.com
RewriteRule ^ http://www.myfiles.com [L,R=301]

.htaccess - redirect links on page by removing folder from link

I followed this tutorial to manage subdomains for a MODx Revo 2.1.3 installation. The idea is that the htaccess file in the web root folder redirects calls for subdomain.mydomain.com to a folder in the web root directory by the same name as the subdomain, ie /home/mydomain/www/subdomain. Subdomain contains the MODx files to run the page, as well as another htaccess to point all further requests back to the root folder.
The better part of it works; I can view the homepage of the site (which means MODx is doing its part), but none of the links to the css, js, images, etc work, and it seems like the Wayfinder and getResources packages are failing to output. The links that are being used for the images+css+js are /subdomain/assets...etc; I need the links to point directly to the /assets folder. It's like the root .htaccess works to redirect the request to the subdomain folder, but the .htaccess in the folder doesn't point anything back up to the root for the remaining requests.
Here's my root folder htaccess, the 2nd part taken from the tutorial:
RewriteEngine On
RewriteBase /
# The Multiple subdomains part
#REDIRECT SUBDOMAIN TO SUBDIRECTORY OF SAME NAME
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ([a-z0-9][-a-z0-9]+)\.mydomain\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
and the subdomain folder htaccess is simply:
RewriteEngine On
RewriteBase /
I know the site works; I can access it using a subdomain that hasn't been processed like the tutorial yet. So it's all there, I just need to sort out the link requests. Can anyone help?
Thanks to some assistance, I managed to find the solution to this issue.
# The Multiple subdomains part
#REDIRECT SUBDOMAIN TO SUBDIRECTORY OF SAME NAME
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ([a-z0-9][-a-z0-9]+)\.spitfireresources\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
#RewriteCond %{REQUEST_FILENAME} !^/js
#RewriteCond %{REQUEST_FILENAME} !^/css
#RewriteCond %{REQUEST_URI} !^/assets
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*) %1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
The key line is RewriteCond %{REQUEST_URI} !^/. Once that was in place everything becomes accessible. Thanks to everyone who commented.

Resources