Jump to content
steveooo

Change $config->httpHost for local development?

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!

Share this post


Link to post
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)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Hello. Watch out: I am talking about $config->httpHost not $config->httpHosts. Last one is an array, first one give the current domain name that.

Anyone an idea?

Share this post


Link to post
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.

Share this post


Link to post
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 :)

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

I've to support Bernhard in his opinion. Why go through the hassle of changing the internal domain name representation, where you don't know the consequences, when setting up the correct hostname for your vagrant machine's ip is so easy and quick. 

  • Like 1

Share this post


Link to post
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!

Share this post


Link to post
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. 

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By jploch
      Hey folks,
      Iam working on a module that extends InputfieldPageTable. I use PageTableExtended as a starting point and all went well so far, except I can't access my config values outside of the module file. The config values allways return the defaults, and not the values in the pw field settings. 

      Here is the code that sets the defaults inside my inputfield module file:
      public function init() { parent::init(); // defaults $this->set('gridMaxWidth', '1600'); $this->set('gridColumns', '12'); } Here is how I add the config field:
      public function ___getConfigInputfields() { $inputfields = parent::___getConfigInputfields(); $fieldsetGrid = $this->modules->get('InputfieldFieldset'); $fieldsetGrid->label = $this->_('Default Grid Settings'); $f = $this->wire('modules')->get('InputfieldText'); $f->attr('name', 'gridMaxWidth'); $f->attr('value', $this->gridMaxWidth); $f->label = $this->_('Container max width'); $f->description = $this->_('Grid container max width in px'); // page name format description $f->notes = $this->_('default is 1600'); // page name format notes $fieldsetGrid->append($f); $inputfields->append($fieldsetGrid); return $inputfields; } Accessing the values outside my module files like this (this gives me the default: 1600, even if I insert another value in the backend field settings):
      $default = $this->wire('modules')->get('InputfieldPageGrid'); $default->gridMaxWidth; Any Ideas?
    • By Sébastien
      Hello everyone,
      I recently started using ProcessWire and already manage to accomplish several things on my own, but I keep asking myself a lot of questions, among which the following:
      After installing a multiple sites version (first method), everything works for now as desired, but I realize that all the files seem accessible by all the domains.
      Let's say that site1.com can access the asset files, template or other ressources of site2.com folders and vice versa by specifying only and for example:
      site1.com/path_to_a_site2_folder/one_file
      or
      site2.com/path_to_a_site1_folder/one_file
      Would you know if there is a simple way to prohibit this kind of behavior in order to clearly distinguish each site and thus ensure the confidentiality and separation of content?
      I prefer to avoid the entities to be considered as a single set by crawlers and prevent access the elements belonging to each domain from another domain.
      Thanks for your help. Sébastien.
    • By Peter Knight
      Hi all
      My .htaccess file is correctly redirecting all requests to
      https:// www. That's great until I want to work locally.
      I thought I had seen a blog post by Ryan where there was a new config setting to ignore both of these if working from localhost?
      I can't find it now so wondering if I was imagining 😕
       
       
    • By iipa
      Hi everybody!
      I have been reading about Multisite, but it kinda bugs me that every topic talks about having both admin and database same for multiple sites.
      I have a project where customer tests it by adding content to the site, while I still need to do some changes here and there in code, maybe some in database. If something crashes for a while, customer can't keep testing, which is a bit problematic.
      Is there any way that I could have two separate versions of one site ("production" and development) that share the same database, but are otherwise independent? Just the thought of having to migrate database every time I want to show client something new gives me anxiety 😁
    • By sins7ven
      Hi community, I am wondering if its possible to display languages based on what host/domain has been requested.
      The use case is the following: I have one installation of PW - and within this installation I set up 5 different languages (default, english, spanish, french, dutch). Now I have 3 different domains (domainA.com, domainB.com, domainC.com) and I want to decide which languages to make available based on what domain has been navigated to. 
      For example:
      domainA.com
       - default (domainA.com)
       - english (domainA.com/en)
      domainB.com
        - default (domainB.com)
        - french (domainB.com/fr)
        - dutch (domainB.com/nl)
      domainC.com
        - default (domainC.com)
        - dutch (domainC.com/nl)
      So what I want to achieve is making only these particular languages selectable in the language switcher on the frontend. Since all pages share the same templates and overall site structure I don't see the benefits of running this installation as a multisite setup or would it be better to do so? But I assume that this would be difficult to handle since the default language might change as well. (On the french version of the page, french should be the default language and so on). Any suggestions how to accomplish that? Thanks!
×
×
  • Create New...