Routing in Laravel and httpd.conf - .htaccess

I've set my document root as follows:
c:/wamp/www/laravel/public
When I test localhost on the browser I get the "You have arrived" page.
However when I try localhost/page0, I get a 404.
In my routes.php file I have this:
Route::get('/', function()
{
return View::make('hello');
});
Route::get('page0', function() {
return 'Test Helloworld! 0';
});
My .htaccess file 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>
It looks like everything should work. Why doesn't this work?

You might want to check if mod_rewrite is enabled by doing:
phpinfo();
You can issue this in your routes.php.
It is also possible to start a webserver in php like so from the terminal or command prompt depending on your OS:
php -S localhost:8080 -t public/
Do this from your project root. Then you can go to:
localhost:8080
And your site should be up and running. You can still use the MySQL server that is running from XAMPP or WAMPP.

Use
c:/wamp/www/laravel/public/page0
This should work.
For production and/or development you can set the document root to the "public" folder, to get rid of the "/public/" in your url:
#see https://stackoverflow.com/a/15469480/1854296 (for example)

Related

Codeigniter and .htaccess mod rewrite

I am having an issue with mod rewrite. I am also a total beginner to it. I am using CodeIgniter and trying to rewrite the following:
http://url.dev/news/news_selection?item=59
to look like this:
http://url.dev/news/news_selection/59/
I have followed a tutorial on: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ (more specifically the : "Patterns and Replacements" section)
The issue is I write my RewriteRule as below and it still does not rewrite the url after going to the page (eg. http://url.dev/news/news_selection?item=59)
RewriteRule ^news/news_selection/([0-9]+)/?$ news_selection?item=$1 [NC,L]
Any advice on where I am going wrong? Thank you.
I am not sure if the routes and controller affect this issue but I added them for reference below.
In my routes for codeIgniter I have
//_News Folder
$route['news/(:any)'] = 'pages/view2/_news/$1';
1: _news is the folder
2: news_selection is the php file
3: pages is the controller
My controller has the following function for view2:
public function view2($sub ='', $page='')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$sub.'/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$sub.'/'.$page.'.php', $data);
$this->load->view('templates/footer', $data);
}
The rest of my .htaccess:
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#When your application folder isn't in the system folder
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
You just need this to use url as above:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|captcha|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You can use this for all controller. This file .htaccess is in application folder.
I figured it out after about 10 hours of trying everything today.
I decided that it may be a routing issue with CI and decided to try passing my variable from the url in the cleaned up format. So...
If I enter: http://url.dev/news/news_selection/59
It will work with the following route:
//_News Folder
$route['news/news_selection/(:num)'] = 'pages/view_test/_news/news_selection/$1';
and controller:
public function view_test($sub='', $page='', $var1='')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$sub.'/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['item'] = $var1;
$this->load->view('templates/header');
$this->load->view('pages/'.$sub.'/'.$page.'.php', $data);
$this->load->view('templates/footer');
}
By using these I can pass the page number as $var1 and then pass it on to my view via $data. Then I picked up $data['item'] as $item in my news_selection.php page and passed it to my model for processing. Reference to CI Manual for this.
After that the page processes like it normally would have.

Laravel 4.2 project not working after uploading - public folder not visible in browser

I've created a laravel project which is working fine on localhost and my hosting server but when I upload it on my client's server it just doesn't work (he's using bluehost).
When I open the URL which is pointing to /public folder, it should display the homepage. Instead it is showing the listing of all files and folders (except public folder).
When I try to open myurl.com/public or myurl.com/public/index.php in browser, it gives following error:
Forbidden
You don't have permission to access /license/public/ on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I can see public folder via FTP and I've changed permissions for /app/storage to 777
Here is my public/.htaccess:
<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>
I've checked my /public/error_log, its showing following error:
[21-Aug-2015 09:06:01] PHP Fatal error: require() [<a href='function.require'>function.require</a>]: Failed opening required '__DIR__/../bootstrap/autoload.php' (include_path='.:/usr/lib64/php:/usr/share/pear') in /home6/parentv3/public_html/eldercareapp/license/public/index.php on line 21
Any help?
Well the problem is with your PHP version. If you see your PHP Version, you might get what is wrong with your application. You must use version greater than PHP 5.5
Ok, I had contacted client's hosting company and asked them to upgrade PHP version. They took a couple of days and upgraded PHP to 5.5.
But the problem was still there even after updating PHP. I wasn't able to access /public folder so the problem was somewhere else. After some searching and trying I've found a solution.
The problem was actually in my /public/.htaccess file. I've added just one line in my .htaccess and it solved the problem: Options +FollowSymLinks
My updated .htaccess in /public now looks like:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
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>
Cheers
Changing permissions to 777 is a terrible idea because it will be vulnerable, instead try with sudo chmod -R 775 project/ where project is the name of your project's folder, then do the next sudo chown -R $USER:$USER project/ to make the project's folder be owned by your user, finally, if you haven't done it, cd into the project's folder and run composer install

htaccess and Laravel

my domain
example.com
I want install Laravel to subfolder
example.com/my_app
when I pass the link
example.com/my_app/public - I see page "You have arrived", but I want see this page without segment "public".
I want make something like I do when I use Laravel not in subfolder, like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I edited .htaccass file in base folder (example.com) :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/my_app
RewriteRule ^(.*)/$ my_app/public/$2 [L,R=301]
</IfModule>
after this when I pass at example.com/my_app/ I redirects to example.com/my_app/public and I see page "You have arrived", but this is redirect (R=301) and if I delete flag R=301 - I see page with "Whoops, looks like something went wrong."
I am three days looking solutions in this website, but ...
tell me what am I doing wrong.
Thank.
The entry point for all requests to a Laravel application is the public/index.php.
for further info you can go to this link.
Assuming you are using Apache, You would need to configure virtual host for this as you would need to point example.com/my_app to your laravel/public/index.php. directory.

what is wrong with this .htaccess and Yii urlManager settings?

I have a yii website under a sub directory e.g
http://localhost/~username/maindirectory/yiiapp/
My htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /yiiapp
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>
protected/config/main.php
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
....
The issue:
let say I accesss :
step one: http://localhost/~username/maindirectory/yiiapp/
step two: click any link on home page (default yii app) let say About Us page
step three: http://localhost/~username/maindirectory/yiiapp/site/page?view=about
step four : click on any on the link (let say same page)
url will look like : http://localhost/yiiapp/site/page?view=about
so : all links are accessible as : http://localhost/yiiapp/.... , instead of removing index.php from link the while string b/w localhost and base directory is removed .
I tried this already and need the same sort of url on localhost , obviously without using sub domains
please help me fix this .
Partial answer:
The problem is that you are redirecting to an absolute URL on the same domain.
The URL probably looks like /yiiapp/site/page?view=about. However that leads to http://localhost/yiiapp/site/page?view=about. You must prepend the website directory relative to the domain (not sure if I expressed properly).
For example, the URL in the a href tag should look like /~username/maindirectory/yiiapp/site/page?view=about so you must prepend the /~username/maindirectory part from somewhere.
Have your tried:
'urlManager'=>array(
....
'baseUrl' => '/~username/maindirectory/yiiapp',
....
http://www.yiiframework.com/doc/api/1.1/CUrlManager#setBaseUrl-detail
so RewriteBase /~username/maindir/yiiapp , in .htaccess fixed the issue , not the
'baseUrl' => '/~username/maindirectory/yiiapp' thing in config/main.php
Here is the full .htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /~username/maindir/yiiapp
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</IfModule>

can't remove index.php with .htaccess

i have installed codeigniter and i want to remove the index.php from url when i access localhost/aplication/index.php. i have set uncomment mod_rewrite.so from httpd.conf en this is my .htaccess file :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
its succes when i acces url from localhost/aplication/index.php/data/users/12 but not with this url localhost/aplication/data/users/12.
how can i fix this??
Try to follow this page about codeignitor pretty urls. Your rules look correct, but I'm terrible with regex.
Some things to look into would be checking if mod_rewrite is available by using phpinfo();. Also step three in that link talks about enabling mod_rewrite using a2enmod. And make sure you restart Apache.
check your config file there should be a line that says:
$config['index_page'] = 'index.php';
You can either set this variable to just '' or you can comment this line out completely, either way it is part of the process of removing the index.php from your codeigniter urls.
this is how you remove the index.php:
htaccess file in the root folder:
RewriteBase /
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
for managing some static pages like: example.com/about , example.com/contact, example.com/terms etc
got to config routes file and edit:
//the controller that will be when it is exampl.com
$route['default_controller'] = 'YOUR_MAIN_CONTROLLER_NAME_HERE';
//I creted this for pages that are created dynamically or for displaying error when I want
$route['404_override'] = 'friendlyPages';

Resources