How do I get x.something.com rather than something.com/x? - web

I was just wondering how to design a website so you can have the addresses for the different pages as I indicated in the title.
So how do you get x.something.com rather than something.com/x?

Add a DNS entry with value of * and url as same as the www url or blank field's url(both which are in fact same and should be)
Then add these lines to your .htaccess file at the root www folder
RewriteCond %{HTTP_HOST} ^x.something.com$
RewriteRule ^.*$ http://www.something.com/x [R,L]
Remember that x is your subdomain and something.com is your domain and besure you have the .htaccess file in the root www folder.
For more just comment...
Thanks

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]

HTTPS forwarding for subdomain

I have a domain, let's make it example.com, and a subdomain, sub.example.com. For the subdomain I have a SSL certificate, for the main domain I don't. On my server I have a .htaccess file for my main domain, but none for my subdomain. How do I specify, in this file, to forward from http://sub.example.com to https://sub.example.com? Or do I need a second .htaccess file in the folder for my subdomain?
I assume your subdomain is hosted in a subdirectory off the main domain's document root? So the main domain's .htaccess file is in the parent directory? (If not, then you will need another .htaccess file in the root of the subdomain.)
Try the following mod_rewrite directives at the top of the main domain's .htaccess file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
This is a temporary (302) redirect. Change it to 301 only when you are sure it's working OK. (Permanent redirects are cached hard by the browser so can make testing problematic.)
You only need one RewriteEngine directive at the top of your file.
UPDATE: If the file structure is as mentioned above (ie. the subdomain is located in a subdirectory off the main domain's document root - where the .htaccess file is located) then you will need to use the REQUEST_URI server variable to reference the URL-path, not a backreference (ie. $1 - as I had initially) to the captured RewriteRule pattern, since the captured backreference will contain the subdirectory, which is not part of the URL-path.

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]

Pointing a subdomain to a subfolder using .htaccess

My webhost automatically forwards all requests to *.mydomain.com to the toplevel domain mydomain.com.
I wanted to map any subdomain to a specific folder on my toplevel domain. i.e. sub.example.com must be mapped to example.com/someFolder (without change in the address bar).
After digging around on the net, I came up with this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.example\.com.*$
RewriteRule (.*) http://example.com/myfolder/$1 [L]
This seems to work well, except for one problem: When I go to the URL sub.example.com, the URL in the address bar changes to example.com/myfolder . But, when I do something like sub.example.com/login - this maps to "example.com/sub/login" properly without the change in the address bar. Any help greatly appreciated!
only small change needed:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?[^.]+\.example\.com.*$
RewriteRule (.*) myfolder/$1 [L]
stripped out http:// at the rule which tells Apache to send a Redirect header instead of proper server-side rewrite.

Resources