Setting subdomains using htaccess - apache

I want to set a subdomain for all site users, like www.companyname.mydomain.com
I would like to use htaccess for this.
when somebody requests www.companyname.mydomain.com it should redirect to
myfile.php?name=companyname
How can I achieve this using an htaccess file ?
Thanks for the consideration.

Make sure this website is configured to respond to *.mydomain.com.
RewriteCond %{http_host} ^www.(\w+).mydomain.com [NC]
RewriteRule ^.*$ /myfile.php?name=%1 [L]
You might want to adjust ^.*$, since this check will rewrite regardless of what comes after www.companyname.mydomain.com.

Related

Need to redirect dynamic subdomain to new main domain

Currently I have a domain with the format of subdomain.domain.com/path, where subdomain is a variable. I want to redirect all of the possible subdomains to subdomain.new_domain.com/path so that if someone goes to subdomain_A.domain.com or subdomain_B.domain.com they get redirected to subdomain_A.new_domain.com and subdomain_B.new_domain.com respectively.
So far I have tried
RewriteCond %{HTTP_HOST} ([.*]).domain.com$
RewriteRule (.*) %1.new_domain.com/$1
to accomplish this but for some reason the regexes will not substitute in the way that I am hoping for.
Can anyone please offer some suggestions or even confirm that this is possible using apache?
Your regex is not entirely correct. You can use this redirect rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.com$
RewriteRule ^ http://%1.new_domain.com%{REQUEST_URI} [L,NE,R=301]

htaccess rewrite rules for subdomain to fetch image directory from main domain

I am new to htaccess configuration but I have been reading and researching for awhile.
I have a mobile subdomain which I wish to fetch images located on my main domain. I am unsure as how to get it to work. My current code is:
RewriteCond %{HTTP_HOST} ^m\.\.website\.com [NC]
RewriteRule ^/avatar/(.*)$ http://www.website.com/avatar/$1 [P]
The image directory looks like this:
/avatar/thumbnail/image.jpg
/avatar/preview/image.jpg
/avatar/full/image.jpg
However, when I try to access m.website.com the images do not load.
Can anyone tell me what I am missing.
Any feedback would be greatly appreciated.
Remove leading slash from your rule and also take out extra DOT from host name condition:
RewriteCond %{HTTP_HOST} ^m\.website\.com [NC]
RewriteRule ^(avatar/.*)$ http://www.website.com/$1 [P,L]
This assumes you have mod_proxy enabled and working.

htaccess redirect for global subdomains system - all subdomains redirected to one php file to handle them all

i really could use some advices related to subdomains.
recently we decide to make every subdomain of main portal to be available for blogs (something similar to lets say any blog platform)
so what i cant achieve is to make redirection to certain folder of main portal site or php file like:
xxxxx.domain.com/index.php?get=xx&get2=xx -> to be run by actual php file located on main portal account, lets say subdomains.php?get=x&get2=xx, alternatively - to be run by mainportal/subdomains/index.php?get=x&get2=xx
i tried with
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.pl$ [NC]
RewriteRule (.*) subdomain.php [L]
effect was almost correct but get parameters are missing:(
on the other hand it can be entirely wrong... i simply dont understand or that rewrite rules:(
thanks for any help/tips
You can try with QSA (query string append):
RewriteCond %{HTTP_HOST} ^.*\.domain\.pl$ [NC]
RewriteRule . subdomain.php [QSA]

Subdomains for redirection only

My domain is meeting.com.
I work with PHP, jQuery, for the main part. No ASP.
When a user creates an account, I'd like a subdomain user.meeting.com to be created for REDIRECTION purposes only. I don't want the subdomain to exist for real.
When someone would load john13.meeting.com, I would like the website to redirect to meeting.com?user=john13 (I don't care if john13.meeting.com remains in the URL bar).
What is the most efficient and easy method of doing it automatically in PHP or Apache? Please consider performance too since I have many users.
You don't need PHP for that. The right Apache configuration with the rewrite module on, will do the trick.
For example (not tested):
RewriteCond %{HTTP_HOST} !^www\.meeting\.com?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.meeting\.com?$
RewriteRule ^$ /user/%1 [L]
Or if you're not rewriting further for SEO, the rule could be
RewriteRule ^$ /index.php?user=%1 [L]
For more inspiration, check out the link Shef posted with another example of how to achieve this.

Mod rewriting? slash to subdomain possible?

can that be possible in mod rewrite? for example (mydomainname.com/myadmin) to (myadmin.mydomain.com)? how to write that in mod rewrite? so whenever the access the mydomainname.com/myadmin they get an error message of not existing.
Thanks!
--Edited---------
Sorry for that. In my website I have an admin (/myadmin) section where only moderators and administrator can access. Now there are a lot of user keep accessing it and I want to change the URL of it. Now instead of 'www.mydomain.com/MyAdmin' it would be 'MyAdmin.mydomain.com'. So whenever they go to 'www.mydomain.com/MyAdmin' they wouldn't find anything.
I just know that htaccess can do url rewriting, But I don't know how to write one.
Thank You!
Try these rules:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /myadmin[/\s] [NC]
RewriteRule ^myadmin - [NC,L,R=404]
RewriteCond %{HTTP_HOST} =myadmin.example.com [NC]
RewriteRule !^myadmin/ /absolute/filesystem/path/to/myadmin%{REQUEST_URI} [L]
That will result in a 404 response when requesting /myadmin. And myadmin.example.com will be internally rewritten to that specific myadmin directory if accessible through the file system.