Need to show subdomain as url than the directory path - .htaccess

I have created a subdomain yesterday as www.subdomain.example.com. This has created a directory at the root by the name of subdomain. Now when i open www.subdomain.example.com, it gets redirected to www.example.com/subdomain. How to keep my url from showing the directory path i.e. in address bar it keep showing www.subdomain.example.com , while showing content from the directory subdomain.
I have a shared hosting account at hostgator.
Thanks for the help

In your .htaccess file, turn off the redirect and use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomain
RewriteRule ^(.*)$ /subdomain/$1 [L]
This checks if the requested host is either www.subdomain.example.com or subdomain.example.com makes sure the request doesn't already start with a /subdomain and appends it otherwise. This redirects internally on the server so your browser doesn't actually get redirected (address bar will still say: subdomain.example.com).

Related

Redirection of Opencart From Domain Name to Subdirectory

I need some help.
I have a small issue. I need if some user enters my domain name in the address bar, for example www.mysite.com, it should redirect to www.mysite.com/abc for example, without requring the user to enter www.mysite.com/abc in the address bar. I am using Opencart version 3.0.2.0. I do not want to install Opencart in subdirectory /abc. www.mysite.com and htaccess file are located in root that is in public_html and subdirectory /abc is not located anywhere, neither in root nor elsewhere. What actually I want is that when user enters www.mysite.com then it should redirect to www.mysite.com/abc and the user should see the same content at www.mysite.com/abc which user would have seen at www.mysite.com.
I have tried adding "RewriteRule ^$ /abc [R=301,L]" at the bottom of htaccess.
With this code www.mysite.com redirected to www.mysite.com/abc but giving an error "The page you requested cannot be found". How can I solve this through htaacess file or in any other way? I would be thankful for any help.
Redirect only the domain's root URL to a subdirectory
Change mysite to your site name. and yourdirectory to your directory name.
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /yourdirectory/$1 [L]

How to redirect (htaccess) any one file on any subdomain to a new single subdomain?

So I have a file
www.example.com/for/ldt.php?do=test
But this file can be found on www1.example.com, www2.example.com and so on
I want to redirect all users no matter what www subdomain the person is coming from to a single subdomain of cdn.example.com
The kicker here is I dont want to redirect for all files or requests, only if they are trying to access ldt.php which is a dynamic script so it can have a bunch of arguments too
Finally, when the change is made with htaccess, is it possible for the url to physically change in the address bar too?
Can all this be done?
You can do it using a rewriteRule that redirects the file based on HTTP_HOST headers or only if the requested host is not cdn.example.com .
RewriteEngine on
RewriteCond %{HTTP_HOST} !^cdn\.example\.com$ [NC]
RewriteRule ^for/ldt\.php$ http://cdn.example.com/for/ldt.php [L,R]
This will automatically append the querystring ?do=test to the end of the destination URL.

How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc

.htaccess - RewriteRule from subfolder to root without redirecting

I'm looking for something in .htaccess to change
subdomain.website.com/page/title
to
website.com/subdomain/title
without redirecting to the website and while keeping the URL in the address bar.
This .htaccess gets called in a subfolder. ublic_html/subdomain. So how do I get back to that folder public_html without using a redirect.
Thanks :)
Here is one way to do it using a proxy, which will leave the URL in the users address bar unchanged.
RewriteCond %{HTTP_HOST} ^subdomain\.website\.com$ [NC]
RewriteRule ^page/title$ http://website.com/subdomain/title [P,NC]

Creating SubDomains to Absolute Paths with .htaccess

Hey, My host is absolutely terrible. For some odd reason creating a subdomain in cPanel simply does not work, and their support lines are always busy. I thought I could get around this by using .htaccess. I'm sure it's not that hard, but I'm kind of new to mod_rewrite and have had little success searching in the last 5 hours. Heres the situation:
/home/user/public_html automatically redirects to http://www.example.com
Since I'm using a CMS in public_html it has already added the rule in .htaccess to redirect anything unfamiliar after example.com/ to a 'Page Not Found'
/home/user/subdomain needs to redirect to http://subdomain.example.com
How should I go about creating a subdomain redirection to an absolute path? Or How can I add an exception in my .htaccess
I doubt you'll be able to get your subdomain to function outside of your public_html folder (although I'm no server admin). Typically that requires DNS modifications or tweaking the server's configuration. Have you tried making a sub-directory and rewriting calls to the subdomain? For example this placed in the .htaccess within your public_html directory:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule (.*) /subdomain/$1 [L]
I'm not sure if that would work (never needed to test it myself), but it's more likely to function than trying to target files that live outside the directory specified by the webhost as the location of your domain's files.
Good luck!
Try this rule:
RewriteCond %{REQUEST_URI} !^/home/user/
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ /home/user/%1%{REQUEST_URI} [L]
But your webserver already needs to be configured so that every request of foobar.example.com gets redirected to this specific virtual host.

Resources