Use one instance of Yourls URL shortner with multiple domains - virtualhost

I've been looking for a way to use Yourls with multiple domains, the main issue is that when configuring Yourls you need to supply the domain name in the config.php file (YOURLS_SITE constant).
Configuring just one domain with actual multiple domains pointing to Yourls causes unexpected behavior.
I've looked around and couldn't find a quick hack for this

I would use this line in config.php...
define('YOURLS_SITE', 'http://' . $_SERVER['HTTP_HOST'] . '');
(note to add any /subdirectory or whatever if that applies)
then as long as your apache hosts config is correct, any domain or subdomain pointing at this directory will work. keep in mind though, any redirect will work with any domain, so domain.one/redirect == domain.two/redirect

I found this quick-and-dirty solution and thought it might be useful for someone.
in the config.php file I changed the constant definition to be based on the value of $_SERVER['HTTP_HOST'], this works for me because I have a proxy before the server that sets this header, you can also define virtual hosts on your Apache server and it should work the same (perhaps you will need to use $_SERVER['SERVER_NAME'] instead).
so in config.php I changed:
define( 'YOURLS_SITE', 'http://domain1.com');
to
if (strpos($_SERVER['HTTP_HOST'],'domain2.com') !== false) {
define( 'YOURLS_SITE', 'http://domain2.com/YourlsDir');
/* domain2 doesn't use HTTPS */
$_SERVER['HTTPS']='off';
} else {
define( 'YOURLS_SITE', 'https://domain1/YourlsDir');
/* domain1 always uses HTTPS */
$_SERVER['HTTPS']='on';
}
Note1: if Yourls is located in the html root you can remove /YourlsDir from the URL
Note2: The url in YOURLS_SITE must not end with /
Hopefully this will help anyone else

Related

Apache 2.4 rewriting directory URLs without trailing slash to https://default_site/dir/ instead of preserving domain

This is a relatively recent behavioral change and appears to be related only to requests which include a "Upgrade-Insecure-Requests: 1" request header.
Apache has started rewriting such requests for sites which are HTTP-only to an HTTPS URL using the default site name instead of just adding the / at the end of the requested URL.
Example: URL submitted in browser: http://www.example.com/blah
Intended redirect: 301 to http://www.example.com/blah/
Instead redirects: 301 to https://default.site.configured/blah/
This happens whether it's a named virtual on the same address as the default server or a virtual using a separate address with separate Listen directives.
I understand all the arguments in favor of the idea that everything should always be encrypted and I don't want to get into a debate about that. This site doesn't consider the tradeoffs desirable at this time.
The default site does have SSL and is configured to redirect HTTP->HTTPS, but the www.foo.com site is not configured that way and does not wish to implement SSL at this time.
Is there any way to get Apache 2.4 to disregard that "Upgrade" header and simply rewrite the URL as desired rather than altering the domain name?
After banging on this some more, I finally found the source of my woes.
This happens when you have IP based virtual hosts and did not configure a name for them using the "ServerName" directive.
tl;dr: If you are having this problem, try adding a "ServerName www.example.com" directive within the VirtualHost definition for the site and that should resolve it.
Details:
It does not happen until you encounter a URL that requires a rewrite other than adding a trailing /. (i.e. if you get a request that doesn't contain the "Upgrade-Insecure-Requests: 1" header, it only gets the trailing / added, but if you get one with that header, it also tries to rewrite the protocol to https which triggers the full URL rewrite).
In my case, the default host name had an SSL configuration, so it didn't fall back to HTTP after the rewrite or reject the rewrite as invalid.
YMMV, I did not continue to do an exhaustive test of all permutations once I found the solution.

OpenBSD's httpd daemon {block} directives not working

I'am trying to restrict access to some subfolders of simple website hosted on OpenBSD's httpd native server. Config is rather simple, it is for testing purposes:
server "10.0.1.222" {
listen on 10.0.1.222 port 80
log style combined
location "/*php*"{
root "/FOLDER"
fastcgi socket "/run/php-fpm.sock"
}
directory {
index "index.php"
}
location "/*" {
root "/FOLDER"
}
location "/SUBFOLDER/*" {block}
}
Inside the SUBFOLDER I placed some htmls not intended to direct viewing.
With last location directive I expect requests like http://10.0.1.222/SUBFOLDER/01.html to be blocked with 403 code but I can't achieve it.
While http://10.0.1.222/SUBFOLDER/ returns access denied, requesting any proper html document name within SUBFOLDER serves that request without any complaints.
If string: /SUBFOLDER/* is (as I suppose) proper shell glob that should match string /SUBFOLDER/ itself + any string given after, then requests like http://10.0.1.222/SUBFOLDER/01.html should be returned with code 403. But it isn't working.
I tried many combinations: "/SUBFOLDER/*", "/SUBFOLDER/*.html" and so on with or without leading /. No effect.
There is probably something I do not understand, but I can't debug my mistake.
What am I missing?
Quick answer for my own question, obtained from misc#openbsd.org: according to the manual man httpd.conf in case of the location statement first match wins. To avoid some more specific rules being ignored it is necessary to put them before more global ones.
In my case putting blocking directive just after log style combined solved the problem.

drupal openatrium (oa2) + muti-domain site (oa_domains) + SSL + base_url in settings.php ERROR

My site is multi-domain site using oa_domains module (in openatrium). All web is under SSL no exception.
Without SSL it works very well (multi-domain or single domain). Configured as https://www.drupal.org/node/2265627 (#4)
To be under SSL, With one domain setting, besides modification in .htaccess (redirect http -> https), I also add base_url to be https ://www.domain.com. This works well.
My problem is that Now I need to configure multi-domain using oa_domains module+SSL, I cannot use base_url anymore (for make SSL work), since the domains are different.
I was thinking to use if statement in settings.php. but some documents say that I should do with directories for different domains.
I then add sites/domain1.com and sites/domain2.com and copy the settings.php to both places but change them with different base_urls. However, it still does not work (errors, page not found, or cannot access the images of theme, plus lots of http:// in the script).
Can I use if statements in settings.php? how to know the current domain name?
Thanks.
I just put
$host = $_SERVER['HTTP_HOST'];
$base_url = 'http://'.$host;
for setting $base_url (settings.php) and
then create symbolic link that domain2.com -> domain1.com.
That's it!.
hosais

Redirect entire site with htaccess to other domain

I want to redirect entire site from one domain to other.
I works when i declare
RewriteRule with R=301 but user can easy notice that
he is redirected to other url in his navi bar.
The result i want to achieve is using remote server to display content
with url keeped from local server.
For example:
User types example1.com, the htaccess gets content from example2.com
(for main page and every subpage in example1.com). Server does not
redirect his browser but just loads remote data from example1.com.
It works when i just use file_get_contentes('example2.com... but
problem comes if there is some $_POST data.
The clean solution is to have the server that handles example2.com respond to requests sent to example1.com. This is usually done by configuring an Apache virtual host to use ServerName example2.com and ServerAlias example1.com.
If you cannot afford to do this, you should rely on an actual proxy-and-reverse-proxy instead of doing it yourself with PHP: it's faster, and it handles absolute URLs correctly (what happens if example2.com returns HTML that contains an <a href="http://example2.com/xxx"> ?). Apache has a correct proxy and reverse proxy implementation.
Not an answer to your question, but this solution is not optimal for a number of reasons:
It depends on two servers being up.
It is slower as the request has to be made twice.
It takes up double the bandwidth, and if you use a PHP script, additional resources on the proxying server.
Are you sure you have to do this?
If you really do, look into Apache's proxying capabilities instead of a PHP script that can deal with the problem you describe (and several others, for example session management, and passing through the HTTP_REFERER and HTTP_USER_AGENT variables). Here is a tutorial for it.
Note that this requires root access to the server.
I don't believe this is possible with .htaccess.
Could you not add example1.com as an alias in the httpd.conf for example2.com and change the DNS records for example1.com to point to the same machine as example2.com ?
That'll achieve what you are after and cuts out the middleman (example1.com's server).
You can't do such a redirect across domains using only mod_rewrite. A rewriterule that writes to an address on another server will always behave as if it has an R flag.
You'll probably have to use some server-side scripting, not sure how though.
I solved it by using:
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => (isset($_POST)) ? http_build_query($_POST) : '',
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example2.com/'.$_SERVER['REQUEST_URI'], false, $context);
I realise that is quite lame solution but it works well.

how to set mod_pagespeed to work on all pages

I've been trying with mod_pagespeed and would like to know if anyone know's how I can add a rule to my httpd.conf that would automatically add all current virtual hosts to the list of running domains:
ModPagepeedDomain http://vhost1.com
ModPagepeedDomain http://vhost2.com
ModPagepeedDomain http://vhost3.com
Thank you.
ModPagespeedDomain seems to accept wildcards. From here:
# Wildcards (* and ?) are allowed in the domain specification. Be
# careful when using them as if you rewrite domains that do not
# send you traffic, then the site receiving the traffic will not
# know how to serve the rewritten content.
ModPagespeedDomain *
Place this in the conf file proper, outside any vhosts.
Yes, you can use wildcards, but please don't use ModPagespeedDomain * unless you can actually control the whole web!
This declaration decides which resources to rewrite and which not to. It is a contract saying that all servers matching the pattern will have mod_pagespeed installed!
Please use something like:
ModPagespeedDomain vhost?.com
Unless you are actually behind a rewriting proxy that can rewrite from any domain.
Also, you can contact us at mod-pagespeed-discuss#googlegroups.com and list issues at http://code.google.com/p/modpagespeed/issues/list