With my current PHP knowledge, I know how to create a subdomain automatically via Plesk api when a user is created on my site. I can then redirect the user.example.com to example.com/user via htaccess.
My question that I cannot figure out how to do, is how to redirect but also keep user.example.com in the navigation bar, not the example.com/user ?
I own a dedicated hosting environment so I have access to change pretty much any setting that I like.
Here are some suggestions:
Don't use .htaccess for this. A very common use of .htaccess is to redirect all requests to a central file (generally index.php).
In order to allow dynamic subdomains you need to make sure your virtual host is configured properly for that.
And in order to redirect to the subdomain url you can use the built-in php function header.
Example:
header('Location: '.$subdomainUrl);
Related
TL;DR is there some way to set the REMOTE_USER variable when bypassing .htaccess login?
I have a login and authentication system running in php on my website. In order to control access to a subdirectory I would like to use http basic auth with .htaccess (I am somewhat limited in my options here because of what my shared hosting instance offers). This answer worked for me for allowing the visitor logged-in with php to avoid having to enter login information a second time in a basic auth dialog.
Bypass .htaccess login when user has a specific cookie SetEnvIf
But I am struggling with one issue now:
The above method works, but htaccess doesn't seem to know the visitor's username. In an ordinary htaccess login, htaccess would set the REMOTE_USER variable upon login.
Is there an alternative way I make htaccess retrieve the username of the php user and so set the REMOTE_USER variable (maybe using a second cookie)? I need this because after auth the visitor should pass through to a mediawiki site.
Here's another question that goes in the direction of what I am trying to solve:
How do I make a PHP variable accessible to .htaccess?
In the case described at the link above, answers suggest making a php variable accessible to htaccess is not possible, because the php and .htaccess are in the same directory. In my case, the .htaccess concerned is in a subdirectory to which the visitor navigates after the php log in. Is it then possible to have the REMOTE_USER set, e.g., on the basis of $_SERVER['REMOTE_USER'] that is set by php?
We have a php project / php web-application where users can create profiles which more or less looks like a website on a sub-domain URLs like robert.blogger.com. Now this user also has a domain of his own example robert.com. Now we want every request for robert.com to redirect to robert.blogger.com without changing the URL.
The URL should show robert.com/home.html, robert.com/aboutus.html etc. but actually code should be run from robert.blogger.com/index.html, robert.com/aboutus.html etc.
Please note that the project is hosted on a dedicated server with dedicated IPs & we also have access to the Control Panel of the user's domain.
We have tried htaccess but that only redirects, we want masking / mapping to work.
Is this possible? If so, how can this be done? Would appreciate much !!!
The solution would depend on what kind of server software you're running, but in Apache you'd do this by mapping the default vhost for the IP to the application (and then letting people point their domain to that host), and in your application use HTTP_HOST in $_SERVER to look up the valid domain (which you're probably already doing to map the subdomain to user accounts). This would be the exact same thing, as long as you keep all links relative in your HTML (and don't think "i mapped it to this user, so the domain should be user.example.com").
To give a more specific answer you'd have to be more concrete in your question.
It should be possible to use ProxyPass in htaccess, if you have permissions to use it. Try something like this.
ServerName robert.com
RewriteRule ^/(.*)$ http://robert.blogger.com/$1 [L,P]
Or you can map it in your application, depends if that server is yours and you can do anything you want to setup.
E.g. I have two domains:
http://www.computer.com
http://www.computers.com
http://www.computer.com is main website and http://www.computers.com is just dummy that will redirect to main website (http://www.computer.com).
So my goal with pure PHP is to detect that user comes from http://www.computers.com rather than google or other website or directly typing url without using refer since it can be disabled.
Both sites are on same hosting, but I cannot access file system of one site from another. And $_SESSION is or $_COOKIE are domain specific variables too.
You could set your redirect script to push to www.computer.com?ref=sister or some similar URL, then log accordingly and, if desired, redirect back to home to keep it transparent to the user.
Are you asking for an htaccess example though? Or just ideas on how to do it?
You say pure php in your computers.com index file:
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.computer.com?ref=sister");
?>
Your best bet is to to address your issues on multiple levels. First you should point DNS for www.computers.com to point at the IP address for www.computer.com. Now both of your requests are being served by the same server or cluster of servers(if behind a load balancer).
Second, if you actually want to rewrite the URL to utilize a single domain (to consolidate your cookies, sessions, etc.), then you could use webserver redirection (i.e. mod_rewrite for Apache) to redirect all the requests to the final domain.
Finally, in that rewrite, you should send 301 codes on rewrite to make sure that spiders and such know that this is a permanent redirection.
I have a website with multilingual support which you can access with virual subdirectory.
For example:
www.domain.com/ru/
which is actually
www.domain.com/index.php?lang=ru
(i'm doing it with .htaccess).
Since I decided to host every language in a different host, I need to make the site use subdomains instead of subdirectory.
However i'm kinda lost here.
I tried to create a subdomain (I use DirectAdmin) and than use .htaccess to do the same thing I do with the subdirectory, but for some reason when I go to ru.domain.com i'm redirected to www.domain.com/index.php?lang=ru...
Thank you very much,
Yolei
I'm trying to create an UnderConstruction page for my new site.
I would like to retain all the core files (files that are part of the site) without modification while I try to implement this. This includes leaving the index.php intact.
Currently, I have an .htaccess setup to authenticate on any access to the site.
I want to redirect any user accessing any page (at least the index.php) to an UnderConstruction page and then leave a link there which my dev team could use to authenticate themselves and continue using the site as usual.
But in order to leave the core files intact, I would have to initiate the htaccess type authentication & then in index.php (assuming that index.php is excluded from the cuth) check the auth status.
I tried to play around with $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] but these (as I understand) requires me to manually implement the authentication scheme (ie checking against a list of username & passwords).
I'm working on an apache with php5 on a linux server.
Any ideas?
You should just mirror the dev version of the site to a subdomain like dev.mysite.com and then keep the under construction stuff on the main domain. This way you can secure the dev domain and still keep your under construction page going and the two will be isolated.
put this on htacces for each file u wanna lock
Redirect /file.extension http://www.uroot.com/index.php