Hi folks,

I've recently got into doing a bit of web development and got hosting for it.

The website is consisted of mainly 3 separate folders(one for home page, 2 for 2subdomains) under the html root directory to avoid cluttering.
To get to my home page on my main domain, I used to do a redirect via a html meta refresh, so i would have an index.html file under the root directory which then redirects into the 'homePage' folder. I have since learnt that this is ugly and not a recommended way to do so.

At the moment I have a ".htaccess" file under the main directory with the following line inside.

Redirect 301 /index.html http://www.mydomain.com/homepagefolder

This works very well when I type in "www.mydomain.com", it will just redirect the client to "www.mydomain.com/homepagefolder". But when I tried to access a subdomain (e.g. "subdomain.mydomain.com"). The client would also get redirected to "www.mydomain.com/homepagefolder".

I would like to know the correct way of doing the redirecting, or if there is a better way to achieve what I'm trying to do. I'm very new to web hosting and web development so any help is appreciated!

Cheers

Dani AI

Generated

A few clarifications and a concise, safer approach that addresses what ran into and what pointed at.

The reason your original Redirect 301 /index.html http://www.mydomain.com/homepagefolder also affected subdomain.mydomain.com is simply that the redirect lived in the same document root the subdomain used (or the server served the same index file for the subdomain). Any request that ends up hitting that index.html will be redirected, regardless of the hostname. The reliable fix is to make the rule conditional on the Host header, or to change the vhost/DocumentRoot so the main domain serves from the folder directly.

If you cannot change the virtual host, use host-aware rewrite rules and test with a temporary redirect first. Example (place in the site root .htaccess):

RewriteEngine On

# only apply when the hostname is the main domain (with or without www)
RewriteCond %{HTTP_HOST} ^(?:www\.)?mydomain\.com$ [NC]

# avoid redirecting requests already targeting the folder
RewriteCond %{REQUEST_URI} !^/homepagefolder/

# skip existing files and dirs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# during testing use R=302; change to R=301 once confirmed
RewriteRule ^(.*)$ /homepagefolder/$1 [R=302,L]

Key points: 1) Use R=302 while testing to avoid cached 301s in browsers, then switch to R=301 for SEO once correct. 2) If you want the browser to keep showing www.mydomain.com/ but serve files from the folder, do an internal rewrite (no R=) or set DocumentRoot to the folder — that keeps a single URL for SEO. 3) Clear browser cache or test with curl -I to inspect headers. 4) If rules don’t seem to run, confirm mod_rewrite is enabled and AllowOverride permits .htaccess.

This keeps subdomains untouched and gives you control over whether the URL changes (external redirect) or the folder is served transparently (internal rewrite / DocumentRoot change).

Recommended Answers

All 3 Replies

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?myDomain.com$
RewriteCond %{REQUEST_URL} !^/folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [R=301]
RewriteCond %{HTTP_HOST} ^(www.)?myDomain$
RewriteRule ^(/)?$ folder/index.html [L]

I ended up doing this, and have it working now. (found the code from ). The code produces 2 temporary redirect rules. I've added a flag for the first one to make it a permenant redirect, but I couldnt do it for the second one. I'm not sure how that will affect SEO...

Look at this code and used it:

The code in the article through the link you provided is for directory a link request for a subdomain to a sub-directory (if I understood correctly) which was already working for me. What i needed was a way to redirect my domain to a sub-directory instead of under root, without redirecting my subdomains into the domain as well.

Member Avatar for Member #949455

What i needed was a way to redirect my domain to a sub-directory instead of under root, without redirecting my subdomains into the domain as well.

A sub-directory is in a folder. To redirect the domain to the sub-directory you just do the opposite. I have a website and also a sub-directory. To redirect I used the same code that I provided above.

But as long you got an answer on your own then everything is good because you found an answer to your issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.