Serving static content from subdomain with codeigniter? - codeigniter

I’m in the process of splitting resources across domains. I’ve read a few different ways on how to handle this. From what I’ve read the correct way to handle is to use absolute paths versus relative. If this is true, how would I handle this as I push my local development live? The domains won’t match from local to live.
The base_url allows for a single domain only as far as I’ve read. Should I create an asset path helper and autoload that? Or, is there something I’m missing here?
http://www.example.com would operate normally.
http://images.example.com/assets would point to my asset folder that exists in the root.

Yeah, you're missing the part where the server configuration points stuff to certain places depending on the request that's made. This is before the request gets anywhere near codeigniter, so take a look at your server software manual and see what's up with that.
An example with Apache virtual hosts might be something like
#httpd.conf:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/http
ErrorLog /var/log/httpd/error_log
TransferLog /var/log/httpd/access_log
</VirtualHost>
<VirtualHost *:80>
ServerName images.example.com
DocumentRoot /var/www/http/assets
ErrorLog /var/log/httpd/error_log
TransferLog /var/log/httpd/access_log
</VirtualHost>
In this example, a request to http://images.example.com/radical.gif would look in /var/www/assets/ for a gif called radical.

You could define a constant in config/constants.php for your assets url and use it:
define('ASSETS_URL', 'http://images.example.com/assets/');
Then of course you would go for it like that:
<img src="'.ASSETS_URL.'image1.png" />

After further research this is the way I solved my problem. Thanks to the Codeigniter forum for helping me get to this solution. Create a switch that checks which stage of development the site is in and set the base_url and asset_url config accordingly.
switch(ENVIRONMENT) //set in index.php
{
case 'development':
$config['base_url'] = 'http://localhost/';
$config['asset_url'] = 'http://localhost/assets/';
break;
case 'production':
$config['base_url'] = 'http://yoursite.com/';
$config['asset_url'] = 'http://assets.yoursite.com/';
break;
}
Then just extend the url_helper and create a function to get the asset_url
if ( ! function_exists('asset_url'))
{
function asset_url($uri = '')
{
$CI =& get_instance();
return $CI->config->item('asset_url') . ltrim($path, '/');
}
}

Related

enable jquery load cross domain with a proxy

I have two resources. A standard apache website on mycooldomain.com and a node website on mycooldomain.com:4337. On the apache vhost I have defined a proxy so that i can reach node using mycooldomain.com/myapp.
Now what I am trying to do is to have this code working in mycooldomain.com/index.html:
$('#somediv').load('/myapp');
but this will end with the browser blocking the request marking it as "insecure".
I assume that the proxy should be the workaround to put the two requests on the same domain but as far as I can see I am missing something.
My proxy definition in apache is the following:
ProxyPreserveHost On
SSLProxyEngine On
ProxyPass /myapp https://mycooldomain.com:4337
ProxyPassReverse /myapp https://mycooldomain.com:4337
What am I missing?
I also tried to add to this:
Header always add Strict-Transpor-Security "max-age=15552000; include subdomains"
the following line:
Header set Access-Control-Allow-Origin "https://mycooldomain.com:4337"
but this didn't solve the issue either.
Any hint?

Reverse proxy for dynamic URL in apache2 server

I want to do reverse proxy for dynamic URLs. Below is my case
abc.xyz.com/p1/#/p2?data=value
to
xyz.com/p1/#/p2?data=value
I have achieved reverse proxy for static path using <Location ''> but not able to figure out how to do it for dynamic URLS
If i'm not mistaken, it doesn't matter what kind of URLs you have.
Could you send your Apache2 vHost Configuration, so I can analyse this further.
My approach would be
[...]
ServerName xyz.local
ProxyPass /p1/ http://abc.xyz.local/p1/
ProxyPassReverse /p1/ http://abc.xyz.local/p1/
[...]
We probably would need more infos. Is the /p1/ Path "dynamic"?
I don't think that I could help you with this. But I'll do my best, if you provide more information :)

How can I handle NotFoundHttpException in Laravel 8?

I'm developing API in Laravel 8. I want to handle NotFoundHttpException to json response. But App\Exception\Handler updated in Laravel 8. By default render method does not exist. So I do not understand what I want to do. I have read in Laravel docs.
You can use a custom callback as per the documentation, App\Exceptions\Handler:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
public function register()
{
$this->renderable(function (NotFoundHttpException $e, $request) {
return response()->json(...);
});
}
If you send the correct accept header with your request the exception handler will return a JSON response already though.
Laravel 8.x Docs - Error Handling - Rendering Exceptions
if you are running on Xamp , edit vhosts where lvauth is your project and directory name
DocumentRoot "C:\xampp\htdocs\lvauth\public"
ServerAdmin lvauth
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
if you are running on Xamp , edit vhosts where lvauth is your project and directory name
VirtualHost lv.auth:80
DocumentRoot "C:\xampp\htdocs\laravel\public"
ServerAdmin lv.auth
<Directory "C:\xampp\htdocs\laravel">enter image description here
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Directory
VirtualHost

Is this a Sinatra config issue, or an Apache config issue?

I haven't decided if this is an Apache issue, or a Sinatra issue, basically, we have a bunch of little apps running on a single server, we deploy them with just built-in webrick instances and use apache to re-route those requests by subdomain to the right port. I am using gollum which is a sinatra app with a git persistence layer, but I am getting an unexpected app routing when it redirects (after edit action). I'm not sure if I can fix this by passing a startup option to Sinatra, or by configuring in the virtual host declaration for the app a rewrite rule. Please in your answer include which option you think is best and an example of how it might be accomplished.
Thanks,
# apache virtualhost declaration
<VirtualHost *:80>
ServerName wiki.domain.com
DocumentRoot "/var/www/html"
ProxyPass / http://localhost:3006
ProxyPassReverse / http://localhost:3006
</VirtualHost>
resolves with sinatra fine for GETs, eg
wiki.domain.com/Home
but fails on sinatra redirect
# expected
wiki.domain.com/Home
# actual
wiki.domain.com:3006/Home
and here is the Sinatra action (source: https://github.com/github/gollum/blob/master/lib/gollum/frontend/app.rb )
post '/edit/*' do
name = params[:splat].first
wiki = Gollum::Wiki.new(settings.gollum_path)
page = wiki.page(name)
format = params[:format].intern
name = params[:rename] if params[:rename]
wiki.update_page(page, name, format, params[:content], commit_message)
redirect "/#{CGI.escape(Gollum::Page.cname(name))}"
end
It’s a long shot, but perhaps the URLs in the ProxyPass directives need to have a trailing /? That’s how it is in the documentation, and I got some strange entries in error.log without them.

Ajax get throws a syntax error when ip is given instead of localhost

i am working on cakephp.
I have developed an application where i have used localhost in all ajax post and get..
like
var ht = $.ajax({
type: "GET",
url: "http://localhost/FormBuilder/index.php/forms/getInvitees/<?php echo $emailid;?>",
async: false
}).responseText;
var myObject = eval('(' + ht + ')');
this thing works only when i put localhost . But when i change that to my Ip like
http://111.11.11.11/FormBuilder/index.php/forms/getInvitees/",
then i am getting a syntax error () in the line
var myObject = eval('(' + ht + ')');
WHy it happens ?? Please give valuable suggestions in solving this..
The response for ht will be
{"invitees":[{"invitee":"23"}]}
from which i will generate a link by
var data = myObject;
$.map(data.invitees, function(i){
var id=i.invitee;
$("<a href=<?php echo $link?>/"+id+"/Invitee> <?php echo $link?>/"+id+"/Invitee</a>").appendTo("#"+inc);
inc++;
return i.invitee;});
Thank you
You know that localhost translates to 127.0.0.1 in almost any case. Did you make sure to setup your webserver to bind to 111.11.11.11 correctly and serve the same DocumentRoot?
Looks like your not getting a JSON object back when calling the server via IP.
EDIT
I don't know whether or not you are using VirtualHosts to set up your development environment but since you're making use of localhost I will go with Apache's standard httpd.conf.
In the httpd.conf file search for a line that start with Listen .... Make sure that the only line with a Listen directive looks like Listen *:80, to allow Apache to bind itself to any of the IPs available on your machine.
Then, insert the following near the end of the file:
<VirtualHost *>
ServerName myfoo.com
ServerAdmin admin#myfoo.com
DocumentRoot "C:/..path to your working directory/"
</VirtualHost>
Now you have successfully implemented a VirtualHost that serves localhost and any other IP your machine is assigned to from the same DocumentRoot (the directory your HTML/PHP/whatever files reside in).
Good luck.
Surely using localhost is more generic in this case?
In my CakePHP apps, I use:
'http://localhost/cakeapp/nodeDescriptors/ajaxSetStatus'
or
'/cakeapp/nodes/updateTreeNodes'
for AJAX calls.
Remember you'll run into problems if you try to access a different domain directly. Perhaps that is what's happening with your IP-based call?
Localhost will only work from your local machine, but mind you, your app will eventually be accessed from out side , where localhost would be the clients machine..
You need to specify the public IP address or the domain name or the network name if you use an internal DNS for an intranet app.

Resources