I have domain as example.dev and i want to redirect it to www.example.dev in laravel 5. In local environment i have setup virtual host as example.dev and also setup it's alias as www.example.dev.
my .htaccess file on laravel 5 looks like this.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
RewriteBase "/"
and my routes file in laravel looks like this.
Route::group(array('domain' => 'example.dev'), function()
{
Route::get('/', function() {
return "I'm root!";
});
});
Route::group(array('domain' => '{sub}.example.dev'), function()
{
Route::get('/', function($sub) {
return "I'm ".$sub." subdomain";
});
});
now if i visit at example.dev, i get output as
I'm root
and if i visit at something.example.dev, i get
I'm something subdomain
and if i visit at www.example.dev, i get this...
I'm www subdomain
what i could think of is to define another custom domain group with www but this is not so good i believe or not good practice. i tried with some modification in my .htaccess file as follow after RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
and now if i visit example.dev it redirect be at www.example.dev but still the output is
I'm www subdomain
then i changes my first routing group from example.dev to www.example.dev and it worked! but i just need to know if this is the right way to do this or not? or using groups for multiple sub-domains is the only way ? and do you know any easy and best way to check subdomains from database through controller so i will check it that subdomain exists then serve with it's view otherwise pass not found error. i know somewhere it can be done with filter and using before but don't know exactly!
help would be really appreciated.
thanks :)
Route::group(array('domain' => '{sub}.example.dev'), function($sub)
{
if($sub == 'www' || $sub == ''){
Route::get('/',function(){
return "I'm root!";
});
}
else{
Route::get('/', function($sub) {
return "I'm ".$sub." subdomain";
});
}
});
Related
Laravel new project routing not working? This is the routes.php file:
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('ID/{id}',function($id){
echo 'ID: '.$id;
});
Route::get('/user/{name?}',function($name = 'Virat Gandhi'){
echo "Name: ".$name;
});
So what i have done is this. I started the local laravel development server with: php artisan serve. Just like the book told me that i am going through (Laravel 5). But now only the first routing works '/' which uses the welcome view blade template.
But all other routings don't work >.<
Can someone please help me? I'm stuck.
My app/public/htacess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I tried your all routes it's working fine.
Note:routes are case-sensitive make sure you are accessing the same route as you mentioned in the route.php
I tried below routes :
http://localhost:8000/user/1
return :Name: 1
http://localhost:8000/ID/1
return :ID: 1
You have to use return not echo
Like this:
Route::get('ID/{id}', function($id) {
return 'ID: ' . $id;
});
I tried your all routes it's working fine.
Note:routes are case-sensitive make sure you are accessing the same route as you mentioned in the route.php
I tried below routes :
http://localhost:8000/user/1
return :Name: 1
http://localhost:8000/ID/1
return :ID: 1
Note : make sure you run apache & mysql services from xamp before you serve you project.
Sorry i'm not able to tell this in the comments above as i don't have enough rep.
I think the problem is your mixing the servers you are using. First try launching your LAMP server and start apache and mysql. Since you've said that the apache launched by LAMP is pointed at port 80, you can try and access it directly on the browser (e.g. http://localhost/yourproject/public/user/1).
I'm guessing since you're using LAMP, your project is under the www root. That's why i've entered the full path for accessing the user route.
I'm using this code to implement admin area routes but Route::get('/',...) doesn't work, it seems I should use anything other than / in get ,otherwise laravel doesn't load view when I browse to mysite/admin/.
Route::group(['prefix' => 'admin', 'namespace' => 'admin', 'as' => 'admin'], function() {
Route::get('/', function() {
return view('backend.index');
});
Route::resource('post', 'PostController');
});
UPDATE: there is an admin folder in public that is public/admin. It seems Laravel open this directory instead of going through the route !
is it normal ? does public folder structure has priority to Route::get() ?
If you have admin folder inside public folder it's normal that this directory content will be displayed but it's not Laravel issue.
If you look in public/.htaccess you have there something like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
so if directory or file exists in public directory Laravel will not launch application but server will display this directory or this file. This is what should be done, because if there wouldn't be such rule no CSS files, JavaScript files or images could be displayed.
What you should do is either change directory name in public folder from admin to something else (and then make changes in your code to reflect this change) or change admin route to something else
My laravel routes are not working at all. I tried something like this:
Route::get('welcome', function () {
return View::make('welcome');
});
Accessing it with localhost/project/project/public/welcome works fine. I have tried it in many ways but seems like routes aren't working since localhost/project/welcome show me 404 error. I know there is simillar topic but there is no answer for me. Could somebody help me out please?
My htaccess file looks like this (I have never touched it):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
You need to edit your HTTP server to have the document root as project/project/public/
For example in Apache you can do something like that:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/project/project/public/
And in Nginx it will like that:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/project/project/public/;
You have said that you can access your laravel app throught
localhost/project/project/public/welcome. This is because this path is your starting root path, from where you start your application.
Example that will allow you to access new route
Route::get('other_route', function () {
return View::make('welcome');;
});
This code can be accessed, if you will type localhost/project/project/public/other_route into your browser
localhost/project/welcome won't work because your application is deeper than this path.
You should setup virutal host for your application so that your path could be myapp.local/welcome
myapp.local/other_route
Or access your application, assuming that your start point is http://localhost/project/project/public/
I'm experiencing some unexpected behaviour in my laravel (4.2) routes.
Let's say my server is available at https://example.com. If I enter https://example.com/whatever/index.php I would expect laravel to throw a NotFoundHttpException, because a route to "whatever" is not defined. Instead laravel shows me the start page, indicating that my "home" route was catched.
If I solely enter https://example.com/whatever everything is fine (i.e. I get the NotFoundHttpException as expected). I neither have the problem on my localhost. Here https://localhost/laravel/whatever/index.php throws the NotFoundHttpException as expected.
My routes.php file:
// Home
Route::get('/', array( 'as' => 'home', function() {
return View::make('home');
}));
Maybe someone can give me a hint where to start searching what's causing that behaviour: Apache config, PHP config, Laravel config?
Ammendment as answer to S. Safdar:
At first I thought of a .htaccess redirect issue, too. In laravel's public folder (the web servers root dir) lays a .htaccess as follows:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
As you can see, the redirect here is handled correctly. The requested "whatever/index.php" is no real file therefore it is redirected to be handled by laravels index.php. If I remove the Rewrite Conditions (plus Rule) I get a regular apache 404 error page. In my case that's of no help as I want laravel to correctly(!) handle all error pages. But for some reason laravels home route matches every url ending on /index.php.
It seems this issue is present with both nginx and apache servers. A few steps I took to mitigate the issue with apache:
Change the index.php filename:
public/index.php to public/start.php
Change .htaccess to read:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ start.php [L]
Change the reference to index.php in the server.php file to have:
require_once $paths['public'].'/start.php';
why not you your route use this like?
// Home
Route::get('/', function() {
return View::make('home');
}));
i think problem is coming from as that you used in your route array.
I 'think' I have successfully installed Laravel using 2 different internet guides. Here is one one of them
http://phpraxis.wordpress.com/2014/07/04/getting-started-with-laravel-4-on-ubuntu-installation-and-configuration/
I have managed to get the include laravel test page "You have arrived"
I am now unable to add an additional route. This is what I did in the routes.php file
|
*/
Route::get('/', function()
{
return View::make('hello');
});
Route::any('foo', function()
{
return 'Hello Andrea';
});
If I add a page foo or foo.php I get the error
Not Found
The requested URL /foo was not found on this server.
I can alter the code in the default view
However if I do something like this
Route::get('/', function()
{
return 'Hello Andrea';
});
everything is fine
I'm confused. I don't think I get Laravel views. how would I fix this to work as a first step on my jourmey
Route::any('foo', function()
{
return 'Hello Andrea';
});
Some guides also suggested putting Laravel in the home folder e.g. /home/username/laravel and whan I tried this I couldnt even get the 'you have arrived page'
I thought Laravel had a shallow learning curve? It must be me.
Thanks for any help
Edit
Ihave found a page that says that conf.d in the PHP file is the old way and it should only be on the other directories on the same level~: apach2 and cli, and now you make the sym link in apache2 and cli. Any opinions much appreciated?
Thanks
You need to make sure that you have mod_rewrite for Apache on and you have in your public folder the following .htaccess file (Laravel defaults):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
go to the httpd.conf file of the server and remove hash from line showing below
LoadModule rewrite_module modules/mod_rewrite.so