Laravel Ngrok and absolute URLs - laravel

I used this command to startup ngrok for a laravel site, namely testsite.local:
ngrok http -host-header=rewrite testsite.local:80
I have testsite.local defined in /etc/hosts to map to 127.0.0.1
This works, Ngrok starts up just fine and now serves the local site on some random *.ngrok.io address, which I can access. But all URLs within the laravel application (e.g. internal links, or urls for loading a css or js file) are absolute urls to my locally defined domain, like http://testsite.local/news, or http://testsite.local/css/styles.css. In other words, I can load the site fine, but anyone else just sees a bunch of unstyled html and gets a non functional site.
This has to be a general issue for anyone who uses ngrok and has absolute URLs within their project, but google didn't yield anything useful.
Two possible approaches come to my mind:
rewrite all links in the application to be relative instead of
absolute (oh god please no)
any client that wants to access my site via the *.ngrok.io url has to map the 'testsite.local' domain within their very own /etc/hosts file to the ngrok.io url.
The approaches may work, but this seems so far stretched... isn't there anything else one can do?
SOLUTION
https://stackoverflow.com/a/54488972/718980

You need to use Apache Module mod_substitute.
First, enable module:
a2enmod substitute
service apache2 restart
Then, add the following to the .htaccess file:
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|<your-local-link>|<your-ngrok-link>|in"
More information: https://httpd.apache.org/docs/2.4/mod/mod_substitute.html

In xammp/apache/conf/httd.conf
DocumentRoot "C:/xampp/htdocs/{your-project-path}"
Directory "C:/xampp/htdocs/{your-project-path}"

In the httpd.conf set your DocumentRoot and Directory to the path of your project.
DocumentRoot "C:/xampp/htdocs/projectName"
<Directory "C:/xampp/htdocs/projectName">

Related

Laravel website behind the reverse proxy and subfolder?

I have a Laravel website with LAMP stack and host on a ec2 server. I setup the Virtual Host configuration for the host name www.abc.com and it works fine. One day, The client said they want to use their domain to visit the website like www.client.com/abc. They use the reverse proxy to point our ec2 server. The first problem we met is the virtual host has failed and we fixed it by adding server alias setting.
ServerAlias www.client.com
The url has connected to the server correctly and the second problem is coming up. Because of the subfolder of the url www.client.com/abc, all the assets paths are failed. For example, we had fonts, styles and js paths that generated by laravel-mix:
{
"/js/app.js": "/js/app.js",
"/css/app.css": "/css/app.css",
"/js/login.js": "/js/login.js"
}
Those paths are absolute and it turns to 404 found on the website if we use the mix() helper function to load assets. The rendered url on the blade pages are wrong.
It would be
http://www.client.com/js/app.js
instead of
http://www.client.com/abc/js/app.js
And the fonts loaded from the vendor and styles, which use the relative paths and are converted to absolute as well. So we had to workaround this by adding the option processCssUrls: false to webpack.mix.js.
The third problem is the redirection when user logs in out. The defaults redirect url is /. I know we can change the redirect urls in middleware and controllers but it feels like so much workaround over this. Is there any better way to solve those problems?
To declare the question again, how to setup a Laravel project behind the reverse proxy with subfolder?
http://www.example.com/laravel

Images from website not working for server root

I am hosting multiple websites on the same server. Instead of uploading the (same) pictures for each website into individual folders, I would like to make ONE main folder on the server where all websites will get their image from, so I dont end up with duplicates.
I tried everything but cannot seem to get it working. Can anyone help me out?
Hosting on Ubuntu 16.04 with Apache2.
My host file:
Alias "/product-image" "/var/www/uploads"
<Directory /var/www/mysite.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
So basically what I want is when my SRC goes to:
mysite.com/product-image/ferrari/f1.jpg
it should be served from
/var/www/uploads/ferrari/f1.jpg
Tried multiple tutorials but nothing worked so far.
P.S. when I go to the url mysite.com/product-image I would expect to see my upload folder but I see nothing. Instead I get an error:
Not Found
The requested URL /product-image was not found on this server.
Apache/2.4.18 (Ubuntu) Server at bedrijfskledinggroothandel.nl Port 443
If you have your multiple websites set up as subdomains in your hosting (as I do), each website at run time can only see the files in or below its specific subdirectory - the hosting will put this layer of security in place.
If this is your own server, not externally hosted, the same may well apply but you may perhaps be able to override this element of the configuration (if you want to - to me, the reason for this security layer is to prevent users realising the stuff is in the same place and trying to take advantage of the fact in some way).
You could however get to what I think is your objective by putting a http (not file level) redirect in place (via .htaccess) so that the subdomain interpreted https://my-website.com/Images (or whatever) as https://www.my-main-domain.com/central-image-directory, which would do the trick I think.

htaccess problems ("/file/" serves file.php)

I was starting to implement mod_rewrite rules on my site when I came across some weird behaviour. I removed my htaccess file for this test, to take it out of the equation.
My local dev site is at http://dev.mydomain.com and is a virtual host.
If I go to, eg "http://dev.mydomain.com/blog/", that folder doesn't exist, but apache finds a matching php file "blog.php" and instead displays that.
This only happens when there is a matching php file - when there isn't, eg "http://dev.mydomain.com/barfblurg/" I just get a 404.
It's like there are some extra mod_rewrites going on above where the site htaccess would be - that when /file/ couldn't be resolved, it searches for other matching files and instead serves this - but there are no other htaccess files that would have an effect, so this must presumably be a config thing? I can't see anything in the apache.conf or php.ini that would cause this behaviour.
(This also doesn't happen on my live host elsewhere, so it's definitely a config thing.)
Anyone point me to where to turn that behaviour off, because it's interfering with the url rewrites I want to do?
(Apache2, OSX, 10.10.5)
This behavior is due to enabling of option MultiViews.
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
To turn this off use:
Options -MultiViews
at top of your .htaccess or in Apache config/vhost file.

What kind of server configuration is needed to have a laravel app working

Using cpanel I have upload into a server a laravel application. During the development I have used MAMP and it works fine.
When I upload the application into my host server, I only can see the first page:
http://myurl.com/appname/public/
When I try to navigate I always get this error:
Not Found
The requested URL /myurl.com/appname/public/account/sign-in was not found on this server.
What is the problem? Are there any special configuration for laravel applications?
Edit:
.htaccess file content:
Options -Indexes
SetEnv DEFAULT_PHP_VERSION 55
I tried to add RewriteBase /like this:
Options -Indexes
SetEnv DEFAULT_PHP_VERSION 55
RewriteBase /
But it didn't work either.
I'm using this host https://www.ecowebhosting.co.uk/?page=webhosting&tab=advanced
You didn't mention which web server you're using, but if it's apache, it looks like you need to either
Tell Apache to process .htaccess files (so it can see the rewrite rules that pass everything to index.php
Turn on the mod_rewrite module, which is the module that does the work of rewriting URLs
When you request a URL like
http://example.com/appname/public/account/sign-in
in a Laravel application, the rewrite rules turn that in
http://example.com/appname/public/index.php/account/sign-in
Behind the scenes. It looks like your server isn't setup to do this. Also, since you're serving Laravel out of a non root folder, you may need to set the RewriteBase. However, generally speaking, the ideal Laravel setup is one where the public folder is your web root. If possible, I'd configure your server so that's true as well.

Custom URL's to users, dynamically at a scale

I need to create custom urls for my users, for ex: www.example.com/alpha, www.example.com/beta. I was creating symlinked directories, which I think worked as a good starting point for prototyping. However, now I need to do this at a scale, running on several web servers behind a load balancer. I am not sure on the right way to do this.
If you are running on an Apache server and you have access to .htaccess files then try this in a .htaccess file
RewriteEngine On
RewriteRule ^(.*)\.html$ index.php?p=$1
This will point all URL's to index.php?p=* internally.
A user will see the .html, and the server interprets it as the pointed url.
Example: yourdomain.com/cocacola.html -> yourdomain.com/index.php?p=cocacola

Resources