How to execute a script on all subdomains - apache

I have a domain with subdomains witch are used by bunch of people... The thing is that I can't understand wich subdomains are in use by the right people. So my question is:
How to execute a javascript alert with a message(in all subdomains) to users to send me a email if subdomain is in use...
Sorry for my bad English and bad explanation.

Try to use (in .htaccess file):
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain.maindomain.com$ [NC]
RewriteRule ^(.*)$ message.php [R=301,NC,L]
For each subdomain and make the message.php say the message.
I am not good at .htaccess but it might work.
This code would dissable the subdomain during the time you use it tho, so make sure you cange it back.

Related

.htaccess forward 'www.w.' to 'www.'

I have a weird issue where Google is indexing several of my site's pages as 'www.w.example.com' which is causing issues with my security certificate.
While I'm looking for a solution, I'd like to redirect this using .htaccess but I can't seem to get it to work.
Currently I have:
RewriteRule ^(.*)www\.w\.(.*)$/$ https://www.$1/$2 [R=301,L]
But it doesn't seem to work..
I have multiple domains for this site so ideally it needs to redirect to the correct domain e.g. https://www.example1.com or https://www.example2.com
It is a bad SEO practice to have the same content available on more than 1 URLs.
You need to decide on the best URL you would like to use and then do a 301 redirect of the others to it.
You need to use a RewriteCond for matching host name:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.w\.(.+) [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]
However you might still get SSL cert warning because cert negotiation happens before mod_rewrite rules are invoked.

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.

Redirecting all traffic based on Host name

An external server (I'll call it "sub.origin.edu") redirects all traffic to my webpage. I want to take all traffic from this host, and redirect it to a different site (which I'll call "http://foo.target.edu/board/").
My .htaccess file is:
RewriteEngine On
RewriteCond ${HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board/ [R=302]
This doesn't seem to be working. I've confirmed (using PHP) that the host is indeed sub.origin.edu, and the .htaccess file is in the right directory, but this rule just doesn't come into effect. Any suggestions? Thanks.
(If I remove the RewriteCond, the redirect happens, so I can confirm that everything but the rewrite condition is working.)
Use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board$1 [R=302]
You used the wrong substition character ($ instead of %)
I found this question while trying to complete a re-derict for specific hostnames.
This link was of great help to understand how RewriteCond and RewriteRule work.
http://httpd.apache.org/docs/2.4/rewrite/intro.html
If sub.origin.edu is doing a 3xx redirect, then the browser will issue a new request to your server using your.server.edu as the host name. So this rule will never match that. If this is the case, there's no easy way to tell where the request was redirected from.
If they're using a CNAME, Femi has the correct answer.

Setting subdomains using htaccess

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.