I am using Laravel 9 and I am deploying my web in a windows VPC with IIS server. How do I change my named route variable to my domain, without the port.
Here is my route web.php
Route::get('/register', [AuthController::class, 'register_view'])->name('register_view')->middleware(HasToken::class);
In my public view page I use the route in my hyperlinks like this
home.blade.php
Register
The I use the cmd php artisan serve --host="mywebsite.com" --port=3000
*I have to use other port besides 80
Website at my domain load the page perfectly, but my links hrefs are using http://localhost:3000
How do I change this named route settings to be my domain?
I have changed all the constant such as APP_URL in .env to my domain
Have tried using Route::domain("mywebsite.com")->group(function() { /* My routes */ }), but in the web it becomes http://mywebsite.com:3000/routes (it appended my local internal port and also without https), how do I configure this?
Thanks in advance.
Related
I'm trying to deploy a Slim v3 project on a dockerized nginx reverse proxy on my dedicated server (let's supose the URL is www.mywebsite.com) and I assigned a 'subdirectory' ussing the reverse proxy (for example /my_project).
The problem is that when I want to go to a route diferent to / in the 'subdirectory', the subdirectory part of the URL is deleted.
For example, when I go to www.mywebsite.com/my_project it works fine, but when I want to go to the route www.mywebsite.com/my_project/register the browser redirects me to www.mywebsite.com/register, deleting the 'subdirectory' part of the URL.
I've already tried to force a base url but without success changing the APP_URL variable on index.php
An example in Laravel of what I want to achieve with Slim
I've made deploys on Nginx Reverse Proxy using the Laravel Framework and I faced this issue, but I solved it defining the APP_URL in .env file like www.mywebsite.com/my_projectand forcing the root url using the next code
public function boot()
{
URL::forceRootUrl(Config::get('app.url'));
if (Str::contains(Config::get('app.url'), 'https://')) {
URL::forceScheme('https');
}
}
I hope that this example of Laravel helps me to clarify the problem i'm facing.
What is the proper way to add a subdomain into your routes? I have a laravel/homestead project working on my local computer but when I move it to HostGator, a shared host server, it no longer works. Well the home page works but the links to the sub-pages don't.
For example, the following route works fine on my local version:
Route::get('/appointments', 'AppointmentController#index');
But if I use that route on the remote server, it takes it to tekknow.net/appointments instead of tekknow.net/medaverter/appointments.
So I followed instructions from here:
https://laravel.com/docs/5.6/routing#route-group-sub-domain-routing
and added a prefix like this:
Route::prefix('MedAverter')->group(function() {
Route::get('/appointments', 'AppointmentController#index');
});
But that doesn't work either. It also goes to /tekknow.net/appointments
I also tried changing the route to this:
Route::get('/MedAverter/appointments', 'AppointmentController#index');
But that also went to tekknow.net/appointments
Anybody know what I'm doing wrong?
EDIT: I went onto my HostGator cPanel and looked at all my subdomains and saw that my subdomain root was medaverter.tekknow.net which is linked to Document root of medaverter.tekknow.net/MedAverter which gets redirected to http://www.tekknow.net/MedAverter. So I renamed my folder from medaverter to MedAverter to match the subdomain redirection.
Here is a screenshot showing what I see in cPanel for columns Subdomains.Root Domain, Document Root, and Redirection
When you try php artisan route:list | grep appointments,
it prints:
[~/www/MedAverter]# php artisan route:list | grep appointments
| | GET|HEAD | MedAverter/appointments | | App\Http\Controllers\AppointmentController#index | web |
That means your route MedAverter/appointments is working for laravel.
Error 404 means route cannot be found.
So I think that's something wrong with your nginx configuration.
When I try http://tekknow.net/MedAverter/MedAverter/appointments.
It has really found the route with error 500.
So, I think you have defined this code in your nginx configuration:
location = / {
rewrite ^ https://tekknow.net/MedAverter;
}
Solution 1:
Change back to rewrite ^ https://tekknow.net/; in nginx configuration.
(I'm not familiar with hostGatar cPanel, but I think you can change medaverter.tekknow.net/MedAverter redirected to http://www.tekknow.net/`)
And in your laravel project, you need to keep prefix MedAverter to appointments.
Solution 2:
Keep the rewrite code. It means you don't need to change the cPanel redirect.
And remove prefix MedAverter in laravel routes. HostGatar(Nginx) will automatically redirect with this prefix MedAverter for appointments.
Clear all caches
The problem you're getting can be due to cache. So, make sure that's not the problem by running
php artisan route:clear && php artisan view:clear && php artisan config:clear && php artisan cache:clear && php artisan clear-compiled
Base URL
If you want to change the base URL of your Laravel app, that's something already asked.
Multiple Path Prefixes
In this case you have a more than one route with the same prefix in their path. If that's the case, you can use route groups to simplify the structure.
Route::prefix('medaverter')->group(function () {
Route::get('/', function () {
// Path /medaverter
});
Route::get('appointments', function () {
// Path /medaverter/appointments
});
});
This would go to, respectively, tekknow.net/medaverter and tekknow.net/medaverter/appointments. You could also create another prefix, like testing, configure in the same way and go to tekknow.net/testing or tekknow.net/testing/whatever.
Namespace prefixes
When you’re grouping routes by route prefix, it’s likely their controllers have a similar PHP namespace. In the medaverter example, all of the medaverter routes’ controllers might be under a Medaverter namespace.
Route::namespace('Medaverter')->group(function () {
// App\Http\Controllers\Medaverter\AppointmentController
Route::get('medaverter/appointments', 'AppointmentController#index');
});
u can use route groups for that here is a sample try this:
Route::group(['prefix' => 'medaverter'], function () {
Route::get('/appointments', [AppointmentController::class, 'index'])->name('index');
});
or
Route::group(['prefix' => 'medaverter'], function () {
Route::get('/appointments', 'AppointmentController#index');
});
I have a laravel project which I run from my local apache server directory.
The link for accessing this project is
www.localhost/project.dev/public/index.php
And I have a navigation menu
After I have set the APP_URL in .env file to
http://localhost/blog.dev/public/index.php/
I get no problems while navigating through the About and Contact pages in the project but when I access the Home page the browser goes to the
http://localhost/
but not to the
http://localhost/blog.dev/public/index.php/
How can I fix it? Here are my routes:
Route::get('/', 'PagesController#getIndex');
Route::get('about', 'PagesController#getAbout');
Route::get('contact', 'PagesController#getContact');
I think the best way is to setup a vhost in apache. if you want to work like this in your .env set the APP_URL= http://localhost/blog.dev/public/
Run php artisan serve in command line. The laravel buil-in server will active. And you will able to access your laravel application using correct url as like http://localhost:8000 try this and let me know if you have any problem.
I just installed laravel following the instruction on Larvel docs. I chose to use install via composer create project command.
In the routes. php i created a dummy route
Route::get('/', function(){
return 'Front Page';
});
When i access http://localhost/mysite/ It shows directory listing of mysite folder. However when i use http://localhost/mysite/server.php It runs my route closure.
I also tried alternate .htaccess code provided at Laravel's docs but that doesn't work either.
I want to remove the server.php from url.
Thanks in advance for help.
This behavior is expected and this is how Laravel works. The public folder is meant for assets and is also (supposed to be) the webservers root directory.
If you are working on localhost that is not the case and the root directory contains multiple projects.
In order to get rid of public you would have to change virtual host settings.
As mentioned here in the site.
The problem with doing virtual hosts is that other projects in localhost will become inaccessible.
I added this to my routes
Route::get('account/create',array(
'as' => 'account-create',
'uses' => 'AccountController#createGet'
));
then when i go to public/account/create , i got a
The requested URL /webproj/public/account/create was not found on this server error.
Then when I tried it to my /webproj/server.php it works.
You have some problems:
1) If you still need to use /public in your URL, the correct form would is:
http://server/project/public/index.php/account/create
2) You should not be using /public, so you need correctly configure your webserver virtual hosts, to point directly to /public so you can just access your urls as
http://server/project/account/create
3) /webproj/server.php should not be accessible and should not be used, Laravel needs it only for running
php artisan serve