Taig
Taig

Reputation: 7298

Define base url for laravel application

My client's application is temporarily running on my staging server (my-domain.com), while on his server (client-domain.com) I placed an .htaccess file to redirect all requests to my staging server. This works fine so far except when building URLs with Laravel's helper.

When I call {{ action( 'Controller@getIndex' }} for instance, Laravel returns my-domain.com/index (instead of the desired client-domain.com/index).

For now I solved this by replacing these calls with absolute, hard coded urls but that is disturbing my workflow.

So, is there a way to force Laravel to use a specific base url?

Upvotes: 0

Views: 5525

Answers (1)

Atrakeur
Atrakeur

Reputation: 4244

Laravel generate url for the current domain it is running on. Currently, laravel is running on my-domain.com even if you are calling it using client-domain.com. That depend on how you are redirecting of course.

So, firstly why are you redirecting the client domain to your server.

If it's a permanent redirection, then it's better for you to:

  • consider it just as it is: a redirection
  • consider changing your architecture to support both domains equally (no redirection so)

If it's just for testing/debugging (or whatever) and this redirection will go away when you will move into production stage, you don't have to worry about that:

  • laravel is creating links to avoid doing the extra, useless redirection on each page load, in fact it's a good thing performance wise.
  • when you'll go into production an install the app directly in client-domain.com, laravel will make all links points to that location automaticly, so you'll be safe to remove your app on my-domain.com

On a side note, it's not really recommended to have the same content accessible with multiple urls. So, you have to 301 redirect one domain to the other (seems to be the current behavior) and then use only one of them.

Edit: and to answer you last question, there is no way to set the base url in laravel (for now at least). There seems to be an issue on github about that (here: https://github.com/laravel/framework/issues/92 ). Maybe you can investigate in that direction, but I don't think it'll be implemented one day because that seems to be hacky and quite uncommon in any regular web application.

Upvotes: 1

Related Questions