Jump to content

Change $config->httpHost for local development?


steveooo
 Share

Recommended Posts

Hello,

I am using Antti Peisa's Multisite module. For those people who are not familiar with it. Here is the link: http://modules.processwire.com/modules/multisite/.

It works like this: It gets the current URL that visits the page via $config->httpHost and then redirects to a page who has the same name (e.g. get it via: $pages->get('/www.multi-site.com/')).

The problem: $config->httpHost is locally something like "multi-site.local" and the redirection does not work because the ".com" url is hardcoded as page name.

So I thought: Why not solve this problem by replacing the value of $config->httpHost in a module via:

 $this->wire->config->httpHost = str_replace('.local', '.com', wire('config')->httpHost);

Is this a good way of doing this? And if yes: How can I say that my module loads first in the booting process? Are there any dangers with this approach? If so: any better ideas? I am not a great programmer :)

Thank you!

Link to comment
Share on other sites

hi steveooo,

i'm doing it with some entries in my hosts file (on windows its C:\Windows\System32\drivers\etc\hosts)

192.168.33.10  local.dev  # VAGRANT
192.168.33.10  site1.dev  # VAGRANT
192.168.33.10  site2.dev  # VAGRANT
192.168.33.10  site3.dev  # VAGRANT

192.168.33.10 is the ip of my virtual machine with the webserver but you could modify that to 127.0.0.1 (localhost) and access your server via any domain you want (like site1.dev in the example or also site1.com, demo.com etc)

Link to comment
Share on other sites

You can have a separate config-dev.php besides config.php, which will be loaded if present. Just overwrite the httpHosts array there and not upload it to your live server.

Edit: Actually the config-dev.php will replace the config.php, so it needs to be a full copy with some changes.

Edit2: And I misunderstood it again like last evening.

My suggestion won't of course change your saved domain names in the database. The problem is, that you cannot simply make that work with changing the httpHosts array, as that's not how the multisite module does work. httpHosts is only a whitelist to check against, but the domain does get parsed from various request data. So accessing pw via multi-site.local will either result in an domain multi-site.local or the first domain of httpHosts, if multi-site.local isn't part of the whitelist. So you either need to change the domain names you set up, or you need to include this replacement in the multisite module itself.

  • Like 1
Link to comment
Share on other sites

yes, i have an idea:

hi steveooo,

i'm doing it with some entries in my hosts file (on windows its C:\Windows\System32\drivers\etc\hosts)

192.168.33.10  local.dev  # VAGRANT
192.168.33.10  site1.dev  # VAGRANT
192.168.33.10  site2.dev  # VAGRANT
192.168.33.10  site3.dev  # VAGRANT

192.168.33.10 is the ip of my virtual machine with the webserver but you could modify that to 127.0.0.1 (localhost) and access your server via any domain you want (like site1.dev in the example or also site1.com, demo.com etc)

...or i am misunderstanding your problem.

Link to comment
Share on other sites

Well. I am too using Vagrant and /etc/hosts file on Mac. The thing is: my domain is test.local and even if I change it to test.com (the real-life url) in /etc/hosts, I also need to configure this on another stage environment like e.g. stage.test-phase.com...

Does anyone have some other ideas?

My plan was to modify $config->httpHost (or if in module: $this->wire->config->httpHost). I know want to know if this is a legitimate solution? How can I say that my module executes in PW booting order? I ask this for the simple reason that I want every other module that uses wire('config')->httpHost gets the updated httpHost that I manipulated.

@Ryan? This would be also a great question for you :)

Link to comment
Share on other sites

i still don't get the problem

The problem: $config->httpHost is locally something like "multi-site.local" and the redirection does not work because the ".com" url is hardcoded as page name.

-> make an entry in your hosts file and this problem is solved

Well. I am too using Vagrant and /etc/hosts file on Mac. The thing is: my domain is test.local and even if I change it to test.com (the real-life url) in /etc/hosts, I also need to configure this on another stage environment like e.g. stage.test-phase.com...

sorry, i don't get it. do you want to request one branch (one multisite instance) from different urls?

Link to comment
Share on other sites

Well.

I have three systems:

1. local env (vagrant via url of foo.local)

2. stage system for live testing (stage.foo.com)

3. live system (bar.com)

With your solution, 1.) would possibly be solvable (if I get you right: ich would write something lke "192.168.33.10 bar.com" in my local /etc/hosts), but I would get the downside that if I access bar.com, the defined local IP will be used and not the real IP of the live server.

Also this solution does not consider the stage system.

It would be nice if someone has another solution. Well, my initial question was something like "if changing $config->httpHost, would it be ok?" and your response "where you don't know the consequences" does not help. Do you know the consequences? It would be nice if someone tells me how important this thing is and if I can damage something by changing it.

Thanks!

Link to comment
Share on other sites

It's sparsely used throughout the core mostly when https redirection is needed or if links will be used outside the browser context (e.g. emails). And of course everywhere you used httpUrl. Where you can't be so sure it actually not the core, but any third party modules. You shouldn't really be able to damage things by changing it, as the variable shouldn't be saved anywhere and rebuild on each request. 

Link to comment
Share on other sites

now i get it... What about writing a small script that renames your page names according to your three environments?

Could be a problem when you are relying on the page names somewhere but if you don't that could be an easy solution. Just an idea.

Sorry, I don't know how critical changing httphost your way would be

Link to comment
Share on other sites

Ok. I thank you both for your answers!

Changing the page names is an idea I did not think of... I checked and the page names get written in database (pages field)... So there will probably some problems...

I let this post open for anyone else to write his ideas on this.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...