How to direct load to web folder - php

I have just installed Yii2. I am still new in this framework. When I created new project using composer named "Sample-Yii2", I must open "web" folder to load index.php. I am just curious if this index.php can be moved to root folder. But it cant.
How I can get direct page when I open localhost/Sample-Yii2 instead loacalhost/Sample-Yii2/web?

Use the following configuration in Apache's httpd.conf file or within a virtual host configuration. Note that you should replace path/to/basic/web with the actual path for basic/web.
# Set document root to be "basic/web"
DocumentRoot "path/to/basic/web"
<Directory "path/to/basic/web">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
Reference here. Also read the article.

If you use apache as a web server in development, you should simply properly configure a virtual host in apache config files as described in other . May be it does not worth it. For example if you are just playing around with the new framework (Yii2). Try to use PHP's Built-in Server instead. It's much simpler to use.
cd Sample-Yii2/web
php -S localhost:8000
Go to your browser and visit localhost:8000
For more details see here

Related

Deployed new Laravel project to live server - inner page links do not work

I have recently deployed a Laravel project to my live web server via FTP (Filezilla). Inside my young1.org web root folder I have the subdomain folder bookings, which displays web content at http://bookings.young1.org. Inside that folder I have the folder, 'laravel' that contains my entire laravel application, and inside that folder there is a 'public' directory.
I have imported my local database to one of the database accounts on the live web server via phpmyadmin, and I have switched the 'DB' credentials to point to the new database inside the env file in the laravel project root (changing the following variables: DB_DATABASE, DB_USERNAME, DB_PASSWORD).
When I navigate to http://bookings.young1.org/laravel/public, the home page of my application appears, fine and dandy. However, when I click on any of the internal links (e.g. the login and register) buttons, I just get a series of blank pages, and none of the internal pages appear.
Would anyone be able to take a guess at what the problem might be?
I have tried altering the .htaccess file to look like the below, and changing my 'PATHS' variable inside public/index.php.
Thanks,
Robert
London, UK
// public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
// public/index.php
require DIR.'/../laravel/bootstrap/autoload.php';
$app = require_once DIR.'/../laravel/bootstrap/app.php';
do you have SSH on the server? if yes did u install laravel as you're supposed to, also could you include the .env file contents, you can mask out the DB username and pass and the key you had to generate
never mind my mistake, i clicked on a link and got a SQL connection error (refused)
i still need to know if you installed laravel via SSH or that you just made the public folder the root, because if that's the case laravel cant help you with that (you need a VPS and not a webhost that only supports FTP as far as i know)
and to be sure
BE CAREFULL!!!
you have SQL connection errors that show credentials
Notice that your URLs work fine if you use index.php in them, i.e.:
http://bookings.young1.org/laravel/public/index.php/register
To allow URLs without index.php the mod_rewrite module on your Apache web server must be enabled.
First try to add this line in your .htaccess file of Laravel above RewriteEngine On:
Options +FollowSymLinks
This directive is needed to enable mod_rewrite in .htaccess context.
If it doesn't work after this, then you can check if module is enabled on your web server, the easiest maybe is to paste this in the beginning of index.php in public folder:
phpinfo();
Then open any page and search for mod_rewrite on the page, and see if you can find it under Loaded Modules. If not, you have to enable it.
To do that, if you can access through SSH you can do:
sudo a2enmod rewrite
sudo service apache2 restart
For more help on enabling mod_rewrite on Apache web server, check this answer.

.htaccess, protect the root file without denying access to their childs

I've developed a web application and I'm protecting it from direct access from browser using htaccess/htpasswd. What blocks me is that I want to protect the root directory of my application but not the files which are contained in it.
For example I have root folder 'A' and it contains folder 'B'. And under 'B' I have 'login.php'. I want that browser blocks me if I try "www.myapp.com/A/"
and allow access if I write "www.myapp.com/A/B/login.php". Regards
I finally find the solution. I just put the next line on the .htaccess file which is in the folder that i want allow access directly :
Require all granted
Thanks for All.
In essence you want to HTTP 403 (assuming you're replicating an .htpasswd failure) anything that's not a file in your directory structure - you can do that by adding (something like) this to the .htaccess file in you docroot.
# this is just to be "on the safe side" and shouldn't be necessary
# also this requires Apache >= 2.4
DirectoryIndex disabled
# this will rewrite anything that's not a file to a 403 Forbidden
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* - [R=403]

simple php Slim project errors

I've created a simple Slim microframework project using PHPStorm 8 on Windows 8.1 with WAMP Server installed. All WAMP Server settings are set by default.
I created a new project called pr1
I used 'Init Composer...' and then added some dependencies like slim/slim, slim/views, twig/twig.
Then I tried to create a simple application just like a given example on main Slim page:
I've created file called index.php in my project folder
index.php
code:
require 'app.php';
Then I've created file app.php
code
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
After this I tried to run my project in Chrome and there an error 404 occured.
Then I tried to pass my name through url: http://localhost:63342/pr1/wade and there was PHPStorm error.
After this steps I've tried to close PHPStorm and my project in browser:
and it seemed like there's a typical Slim 404 error,but when I tried to pass my name through url again it gave me this error:
you need to include index.php for example http://localhost/pr1/index.php/test
to get rid of index.php use .htaccess or something equal based on your webserver
All it was because Apachi doesn't know what to do with all browser requests. For this reason I've created a simple .htaccess file:
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
After this I needed to use rewrite_module in my WAMP settings.
It perfectly works with project when I open it in browser through the explorer.
And after this I set up my server like this:
And that allows me to preview all my changes through PHPStorm

Laravel 4 removing public from URL

So, I'm running xampp on Windows. I'm currently trying to get familiar with the laravel framework. Now, when thats pointed out. How can i be able to access my laravel application/website direct within the root?
Example,
What I'm doing now is: localhost/laravel/public/about (to see the
about page)
What i want to do is: localhost/laravel/about
Any good solutions for this? do i need to add a .htacess file on the root folder of laravel? (not the public one).
Any suggestions?
Easiest way is create .htaccess file in your Laravel root with following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
It should be redirected easily.
Reference: https://coderwall.com/p/erbaig/laravel-s-htaccess-to-remove-public-from-url
Here's how I did it.
Edit your Windows Host file - C:\Windows\System32\drivers\etc\hosts
Edit the Apache vhosts file - Drive-Letter:\xampp\apache\conf\extra\httpd-vhosts.conf
Add an htaccess file to the laravel/public folder (if its not already there)
Restart Xampp apache server
Windows can be a real PITA when trying to edit the Hosts file because of the User Account Control. Since I work on all kinds of small hobby projects, I have to edit this file all the time so this is what I do.
Install PSPad. It loads really fast and you can bookmark files for easy loading/editing. Sublime Text also works well if you load the two files I mentioned above and save the workspace as a new project.
Right-click on the PSPad (or other editor) program shortcut and choose 'Run as Administrator'. You cannot save changes to the Hosts file unless you do this.
Open the Windows Host file in the editor. This file does not have a file extension, so you have to choose "All Files" in the File Open dialog to even see the file.
At the bottom of the file, add this:
127.0.0.1 laravel.dev
This tells Windows to point the web browser to localhost whenever you enter laravel.dev in the browser's address bar.
Save the file.
Open the xampp Apache httpd-vhosts.conf file.
At the bottom of the file, add this: (I am assuming xampp is installed at the root of the D: drive)
<VirtualHost *:80>
ServerName laravel.dev
DocumentRoot "D:/xampp/htdocs/laravel/public"
<Directory "D:/xampp/htdocs/laravel/public">
</Directory>
</VirtualHost>
Add an htaccess file to your laravel/public folder (if its not already there).
I think the default htaccess file that comes with L4 looks like this:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Restart your xampp apache server.
Open a web browser and type in the address bar - http://laravel.dev
That will take you to the index.php file in the "public" folder.
To get to the About page, I think the address would be http://laravel.dev/about
Move the contents of the /public folder down a level.
You'll need to update the include lines in index.php to point to the correct location. (if it's down a level, remove the '../').
BEST Approch: I will not recommend removing public, instead on local computer create a virtual host point to public directory and on remote hosting change public to public_html and point your domain to this directory. Reason, your whole laravel code will be secure because its one level down to your public directory :)
METHOD 1:
I just rename server.php to index.php and it works
METHOD 2:
Here is my Directory Structure,
/laravel/
... app
... bootstrap
... public
... etc
Follow these easy steps
move all files from public directory to root /laravel/
now, no need of public directory, so optionally you can remove it now
now open index.php and make following replacements
require DIR.'/../bootstrap/autoload.php';
to
require DIR.'/bootstrap/autoload.php';
and
$app = require_once DIR.'/../bootstrap/start.php';
to
$app = require_once DIR.'/bootstrap/start.php';
now open bootstrap/paths.php and change public directory path:
'public' => DIR.'/../public',
to
'public' => DIR.'/..',
and that's it, now try http:// localhost/laravel/
Set you document root for apache to the public folder, and not the laravel folder. This is the simplest technique and recommended for production environments.
I'm using L5, This works for me fine:
Rename the server.php in the your Laravel root folder to index.php
copy the .htaccess file from /public directory to your Laravel root folder.
-- Thatz it!!!
I've been struggling with this problem too but i've found a simple solution that only requires you to create another .htaccess at the root of your application.
The .htaccess itself should contain this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^{yoursite}.{extension} [NC]
RewriteRule (.*) http://www.{yoursite}.{extension}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
The complete system keeps working but it redirects to the /public folder.
This is how I solved the problem for myself.
Hope it helps!
Cheers.
Add following code to htaccess file. It may helps you.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Add following code in your .htaccess (if not exist create a .htaccess on laravel root directory)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Source : http://tutsnare.com/remove-public-from-url-laravel/
at Source you also get another method to do same.
Update : Preferred way to do it is make change in directory structure which explain in source URL.
just in simple step i did in laravel 5
make .htaccess like this in laravel folder
<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>
then rename your server.php to index.php
that it it will work
or if you just doing local development
run this comman php artisan serve
it will start local server at localhost:8000 (port may vary)
You can use symlinks or edit the httpd.conf file.
Check my answer to another similar question. I hope that it helps.
If you don't wish to go through the stress of configuring .htaccess file,
you could use PHP Built-in Server by doing this:
From your command utility, cd into laravel\public
The run: php -S localhost:8000
After you can access your website by going to:
http:://localhost:8000
works without appending public
See the official manual to learn more:
http://php.net/manual/en/features.commandline.webserver.php
Go to project folder using cmd and type "php artisan serve".
Now navigate to: localhost:8000
I have found geart flow to work with laravel localy.
What you can do is to configure xampp a bit. At your xamp's httpd.conf file you have to find document DocumentRoot and <Directory>. Change root directory to yours laravel public folder and restart apache. Since when you can access your project simplly just typing localhost. Now if you want you can change your host file and rewrite local dns, for example: 127.0.0.1 example.laravel.com and now you can access your project with real url. It may look bit complicated, but it's not.
Alternative to that would be php artisan serve. You can start server on different ports and when re-write hosts file.
You could add some features to improve your workflow even more, for example vagrant or ngrok. You can share your project for live presentation (speed may be issue here).
Need to remove public segment in the larvel4 app
Laravel 4 requires you to put your app code one level higher than the web root, and this causes problems for some developers that are stuck on shared hosting and that doesn’t allow a setup like this. It’s actually really easy to get around it. I read that some L4 specific packages could have problems on a setup like this, but I didn’t experience anything like that with any package yet.
So first install L4 somewhere you like. I liked the article Niall wrote on keeping the base L4 app up to date, so go and check that out: Installing and Updating Laravel 4
I find it’s enough for this example to simply clone the repo (assuming you have composer installed globally, if not, go to http://getcomposer.org/):
git clone -b develop git://github.com/laravel/laravel.git app_name
php composer install
Note that we are cloning the develop branch since L4 is still in beta at this time.
So to remove the “public” part from your URL, simply move all files and folders from public to your app root and you’ll end up with a folder structure like this:
/app
/bootstrap
/packages (copied from /public)
/vendor
.htaccess (copied from /public)
artisan
composer.json
favicon.ico (copied from /public)
index.php (copied from /public)
robots.txt (copied from /public)
server.php
Now we need to edit our paths in index.php:
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/start.php';
And then just set the public dir in out /bootstrap/paths.php file:
'public' => __DIR__.'/..',
this is my suggession
You need to do following things:
first copy all the contents of the public directory in your root directory i.e. bring the contents of public folder 1 step up.
modify the contents of index.php
From =>
require __DIR__ . "/../bootstrap/autoload";
$app = require_once __DIR__ . "/../boostrap/start.php"
To =>
"require __DIR__.'/bootstrap/autoload.php';"
"$app = require_once __DIR__.'/bootstrap/start.php';
and also contents of bootstrap/paths.php
From => 'public' => __DIR__.'/../../',
To => 'public' => __DIR__.'/..',
3.Then finally create .htaccess file in your root directory and write this.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Simple Steps To follow:
Rename server.php (In Root directory) to index.php
Copy .htaccess file from public directory to root directory
rename the server.php to index.php and copy .htaccess from /public is the right way.
If you send your app online,just change DocumentRoot to the path of public.
if you remove public from url first of all move index.php and .htaccess file from public folder to root of the laravel and change in index.php file
require DIR.'/../bootstrap/autoload.php';
$app = require_once DIR.'/../bootstrap/start.php';
to
require DIR.'/bootstrap/autoload.php';
$app = require_once DIR.'/bootstrap/start.php';
and run the program
This has been asked before many times. I had the same problem. I solved it by using vhosts and .htaccess files. I wanted to write about solution on both XAMPP on Windows and LAMP installation on Ubuntu. My configuration on windows:
My aim was to reach my application by entering the uri below
http://localhost/subdir
c:\xampp\htdocs\subdir\ # this was laravel root directory
c:\xampp\apache\conf\extra\httpd-vhosts.conf # this virtual hosts file
I used original .htaccess file from Laravel website (and remember .htaccess file must be in public directory) but I just added one line which is
RewriteBase /subdir (just below RewriteEngine On)
In addition, in httpd-vhosts file you should add your subdirectory alias like that:
Alias /subdir
"C:/xampp/htdocs/subdir/public"
<Directory "C:/xampp/htdocs/subdir/public">
Order allow,deny
Allow from all
Options All
AllowOverride All
Require all granted
</Directory>
Hope everything is clear with my explanation. This answer can be applied on unix based systems easily.

What should I use .htaccess for in Zend Framework 1.9.5 application?

I've written several Web MVC apps but never before in PHP, and I'm trying to figure out the best way to work with Zend Framework v1.9.5.
I'm confused about the role of .htaccess in a Zend Framework 1.9.5 application. I have consulted many tutorials, books, and SO questions on Zend Framework (but most of them are for v1.8 at newest) and they all talk about the central role of the .htaccess file. I gather that .htaccess can be used to support virtual hosts, URL rewriting, and to allow Apache to serve static files without going through index.php but I'm not sure if this is current practice or still necessary in v1.9.5.
Currently I have written a couple of pretty simple (HTML, CSS, jQuery) Zend Framework apps and created Apache virtual hosts for them in a test Ubuntu Server 9.10 environment. I didn't use any .htaccess files at all and the apps seem to work fine.
Here's what I did so far:
I created my apps using Eclipse/PDT and the zf.sh tool. I added css, images, and js directories to the public directory that 'zf create project' produced. These apps run fine on my local MAMP installation.
On the server, I installed the Zend Framework in /usr/local/Zend/share/ZendFramework-1.9.5 and added /usr/local/Zend/share/ZendFramework-1.9.5/library to 'include_path' in php.ini.
I copied the apps to the server directories /home/myadmin/public_html/domain[12]/com.
I created virtual hosts by adding entries in the Apache available-sites directory as outlined in Slicehost Virtual Host Setup. I assign DirectoryIndex = index.php and DocumentRoot = /home/myadmin/public_html/domain[12]/com/public. Both apps seem to work fine.
Do I need to use .htaccess files? For what?
*** EDIT - My Solution
Based on Richard's link, I moved the rewrite statements that usually live in .htaccess into my virtual host definition, and now my applications don't use a .htaccess file. My domain1.com virtual host file looks like:
<VirtualHost *:80>
# Define admin email, server name, and any aliases
ServerAdmin webmaster#domain1.com
ServerName domain1.com
ServerAlias www.domain1.com
# Only serve files from inside this directory
DocumentRoot /home/myadmin/public_html/domain1.com/public
# Directly serve any requested files that exist in DocumentRoot;
# otherwise redirect the request to the index.php script
RewriteEngine off
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
</Location>
# Set up log file locations
LogLevel warn
ErrorLog /home/myadmin/public_html/domain1.com/data/logs/error.log
CustomLog /home/myadmin/public_html/domain1.com/data/logs/access.log combined
</VirtualHost>
One primary use of .htaccess for zend framework and most php frameworks is to redirect all requests (except to static files) to a bootstrap file. You don't necessarily need it but your URL would end up looking something like /index.php/controller/action as opposed to /controller/action
You could also just add the rewrite rules to you apache config directly.
The VirtualHost configuration and .htaccess file are two alternatives for URL rewriting in Zend Framework. They perform the same function and you don't need both.
URL rewriting simply directs all requests for non-static files to your index.php.
Routing is the process of decomposing the request URI and figuring out which module/controller/action should be executed. This is a related but separate function to URL rewriting.

Categories