URL paths work local but not on server - linux

Most of the URL links (css, images, js files etc) in my project are absolute, but these don't work on my server.
I've located the project on my server in a subfolder, so it is going to the root.
How can I change this so the url like /images/background.jpg is going to 123.12.34.56/projectfolder/images/background.jpg instead of 123.12.34.56/images/background.jpg?
I guess it has something to do with Apache config, but I couldn't find it yet...
I'm using Laravel, so maybe there are some laraways to fix this.

Try to take the path url from the root.
If you are using PHP you can use getcwd() function and save it in a variable path root and keep all your URL as relative paths not the absolute paths.
Hope this helps.

what i usually do is create a sub domain (cdn.example.com) and point it to a directory called assets which is in the same level as the public directory. All my js, images, css are in there.
Then in the app.config add a new key value pair 'cdn' => 'http://cdn.example.com'. When i'm adding css or js files i'm using it like this.
<script src="<?php echo Config::get('app.cdn') ?>/js/jquery/1.7.1/jquery.min.js"></script>
Why do this? say if i decide to move my assets to a caching server i can easily do so by moving just the cdn.example.com

Related

Apache Map All Paths to Root Without Redirect

I want Apache to blindly give the root website directory the full URL without a concern for the path. Reason being I have an Angular app that handles routing and it's at the root directory.
I've tried stuff like this:
AliasMatch ^/(.+) /var/www/html/mywebsite.com
But it always results in an infinite loop.
Essentially, I just want to disable path-directory resolution.
EDIT: I should also clarify that I have multiple sites hosted on the same machine and still want that to function. I just don't want directory routing from within a single website.
I figured out how to accomplish what I want while also having the neat side effect of allowing me to still have assets that can be reached through directory navigation:
FallbackResource /
Will use the root directory without changing the URL when no such directory the path specifies exists.

why /public folder for static content in node.js

In node.js with express all the static content is placed in /public folder. Can someone help me understand why do we need to place it particularly in public folder? What if I want to create a folder called 'static' with in 'views' folder and keep all images, styles and script files in it. Because I want keep all thing related to views under one folder. If it is not good way of structuring project please help me in understanding reasons behind it.
You can put your content in any folder you want on the server. All that matters is how you set up your Express routes to serve that public content. You can name the local server folder /public or /static or whatever you want and you can put it anywhere in your folder hierarchy that you want too (like in some project directory). You will just need to set the path in your express.static() middleware to match where you put the content on your server so express knows where to look for it to match a given incoming request.

Allow requests to travel up the directory tree (../) in express.js

There might be an answer to this, but I don't know how to phrase it properly, so I'll just ask.
I use a module, called build-url. It is designed for the browser and provides an easy way to construct URLs with query parameters.
I want to store my website in a folder, called site while not having to type it in the URL. I managed to do that easily. However, when the client asks for the build-url.js file, he receives a 404.
Here's how the folder structure looks like:
node_modules
build-url
dist
build-url.js
site
index.html
In my code, I have:
app.use("/", express.static(__dirname + "/site"));
The problem
When I go to http://localhost:5000/, the index.html file is served and everything is fine. Inside, I include the script:
<script src="../node_modules/build-url/dist/build-url.js"></script>
However, in the browser console, I see:
GET http://localhost:5000/node_modules/build-url/dist/build-url.js 404 (Not Found)
The server looks for a node_modules folder inside the site folder, instead of going up the directory tree. How do I fix that?
It is not a very good idea to allow your web server to send files from outside of the specified folder because it will allow the bad guys to download anything from your server including your confidential data.
You can copy required file to static folder or you can setup gulp or any other automation tool to do it automatically.

Serving static content on a mounted app

I have a mounted app that taking requests on the /myapp/ path. Now I want to serve static content.
The path has to be absolute in the context of my mounted app, but relative to the path of the mount itself.
So, for example, I want the pages on /myapp/something/ and /myapp/asd/somethingelse to all load images, CSS and whatnot from /myapp/. The files themselves are in the public folder with routing setup for serving static content.
The problem is that I can't put relative paths in my base layout template because if the request path is /myapp/asd/somethingelse the browser will look for /myapp/asd/mystaticcontent.css and I can't use absolute paths because the path to myapp might change which would require manual modification every time I move it.
What I basically need is a way to get that prefix that express hides from the mounted app AND NOTHING ELSE.
There's request.originalUrl but that also contains the path that myapp is matching on. I don't know how to reliably strip the request.url from request.originalUrl and it does seem kind of a roundabout way of doing it.
If I had that base URL of my mounted app, I could pass it to the template and then generate an absolute path on the spot.
Edit:
app.path() might be what I want but this isn't documented.

Laravel 4: how to protect assets folder?

I come from CodeIgniter where files/folders are typically protected this way:
.php files
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
index.html files
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<p>Directory access is forbidden.</p>
</body>
</html>
I guess can be possible with .htaccess or hard way but, how can be done using Laravel 4? is there a way using its 'standards'?
Edit: Is a project built for shared hosting.
/assets/{css, img, js}
/packages
/system/{app, bootstrap, vendor, index.php, .htaccess, favicon.ico}
You need to make a file .htaccess, after that you need to add the following:
# Disable Directory Browsing
Options All -Indexes
You should set up your application so that everything outside of the initial public directory is outside of your document root. That's one of the reasons why Laravel actually ships with a public directory. Typically most people will symlink this directory to the document root. Anything inside public is, obviously, public. If you'd like your assets directory to be inaccessible you could opt to use htaccess or an index.html file much like how you've described.
If, for whatever reason, you need to shuffle some things around and have your actual application files within your document root then you'll need to implement some form of security if you see the need. This, again, could either be using htaccess or an index.html file. Typically a htaccess approach is simpler. If you wanted to protect the app directory you could drop a .htaccess file in there that looked something like this.
deny from all
I have provided a similar answer to this type of question here: Laravel image gallery logic
In principle - you should store all your assets outside of public - and use PHP readfile() to securely serve them to users as required.
The answer might be a year late, but I thought it might help others who want to tackle something similar.
Check out Kelt Dockins' post here. The Codesleeve Laravel Asset Pipeline would allow you to have your assets folder outside your /public folder, securely.

Resources