How to access the Laravel Direct Path - php

Am new to laravel, I have install laravel using composer in my linux server, the problem is I couldn't able to access the domainname.com but domainname.com/public/ then only I can able to access the page. Kindly suggest me how to access the site with domainname.com/ instead of domainname.com/public/
Am running in shared hosting so there multiple domain name with folders are available as well.

First, make sure that your rewrite module is enabled. In your case, share hosting providers often enable it for you.
Second, create an .htaccess file in your home folder (the folder contains artisan file) with this content.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
# For all files not found in the file system, reroute the request to the
# "index.php" front controller, keeping the query string intact
<IfModule mod_rewrite.c>
RewriteCond $1 !^(public)
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

You need to add a <VirtualHost> in your Apache configuration:
<VirtualHost *:80>
ServerAdmin admin#localhost
DocumentRoot /path/to/your/laravel/public
ServerName your.domain.com
<Directory /path/to/your/laravel/public>
AllowOverride None
Order allow,deny
Allow from All
Options FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</Directory>
</VirtualHost>
Then you can access your Laravel site as:
http://your.domain.com/

Related

Symfony (3.4) app : cant redirect requests to /web dir via .htaccess

I've a website lets say website.fr ( on server : /web/website ) ;
I ve added a symfony blog app in /web/website/blog/.
I dont manage to access the blog via website.fr/blog/ (404 error)
There is a .htaccess file in /web/website/blog/ directory, and one in /web/website/blog/web directory.
I am used to deploy symfony apps 'alone' on a server, and make the domain point to the web dir of the app, and everything always works fine, but in this configuration, I cant get it to work.
I have tried to use the following .htaccess conf under /web/website/blog/ :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>
And I did not edit the default one under /web/website/blog/web/
Im not sure what Im doing wrong, so any help would be appreciated !
For me I use .. nano /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/web
<Directory /var/www/project/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
which is explained Here and restart Apache, but I do not know if I understand your problem
I couldn't get what I wanted, but here is the solution I implemented :
I created a .htaccess in /web/website dir, that rewrites all requests to /web/website/blog, with the root (/) excluded from this rewriting :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ blog/web/$1 [QSA,L]
</IfModule>
That's not the way I wanted to get my rewriting, but I have the result I expected ; note that it would not be a good solution for a website that would evolve with new modules, routes, folders...

how to write .htaccess and protect sites files and folder in laravel

i developed a website in laravel but problem is if someone know the name of files he can directly access that files of website which i don't want
JobScholar
app
Exceptions
Http
Mail
Models
Permission.php
Providers -
Role.php
User.php
.env
etc etc
application is working fine but what i want if user type
http://localhost:8080/JobScholar/app/
or
http://localhost:8080/JobScholar/User.php/
he should rediret to home controller or see an error message
my .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
Options -Indexes
ErrorDocument 403 http://localhost:8080/JobScholar/index
Options +FollowSymLinks
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /JobScholar/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
## Don't listing directory
#Options -Indexes
#
## Follow symbolic links
#
#
## Default handler
#DirectoryIndex index.php
i don't want the user to access any of the file or directory the only directory and files he can see are only those whose routes are defines
Laravel basic flow is that ALL the requests go through index.php file in public folder. So you have to make sure your server points all the request to that folder so index.php can serve and take care of the rest.
Quick fix is to create vHost file(assuming you're not on shared host).
Here's a sample:
<VirtualHost *:80>
ServerName www.site.com
ServerAlias site.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/site_name/public
<Directory /var/www/html/site_name/public>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
On apache server it will go in:
/etc/apache2/sites-available/
Then enable the site a2ensite site_name.conf
Finally point your site to server IP in /etc/hosts like so: 127.0.0.45 site.com
Also be sure to enable mod rewrite using a2enmod rewrite so .htaccess provided by Laravel can make required rewrites to the URL.
Finally restart apache: service apache2 restart

Rewrite to another document root

I'm working on a new website which is supposed to replace an old one. The new website will use Phalcon and it's using its own routing system. The new website will be accessible using www.mydomain.com, like the previous one. The new website DocumentRoot is /var/www/mydomain-www, the old one is instead /var/www/mydomain-platform.
Inside this "platform" there are some "web services", that many other applications (included an iOS and an Android apps) are using. Unfortunately, the new website must use these web services as well. :-(
Basically there is a PHP file for each API call and all these files are located in the root of the old website, specifically /var/www/mydomain-platform. I know it's a mess but I have to deal with that, I have inherited it. In the old website root there is also a .htaccess file that contains a rewrite route for every single API call, to the right PHP file. They look like:
RewriteRule ^ws/event/games(.*)$ /ws_events.php$1
RewriteRule ^ws/favourites/list(.*)$ /ws_teams_favorites.php$1
I need to find a way to rewrite all the url for www.mydomain.com/ws/(.*) to the directory /var/www/mydomain-platform/ws, because the web services' files are there and I don't really want to move them in the root directory of the new website.
I thought to create a symlink var/www/mydomain.www/ws pointing to the directory /var/www/mydomain-platform/ws, but I'm sure the below vhost configuration will not like it, because it will redirect everything to /var/www/mydomain/public.
I would like to put these redirect stuff inside a .htaccess, and leave the vhost like it is.
Any idea?
<VirtualHost *:80>
ServerName www.mydomain.com
ServerAdmin dev#mydomain.co.uk
DocumentRoot "/var/www/mydomain-www"
ErrorLog "${APACHE_LOG_DIR}/www.mydomain.com-error_log"
CustomLog "${APACHE_LOG_DIR}/www.mydomain.com-access_log" common
AllowEncodedSlashes On
AddDefaultCharset UTF-8
<Directory /var/www/mydomain-www>
AuthType None
AllowOverride all
Require all granted
<IfModule rewrite_module>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [B,NE,L]
</IfModule>
</Directory>
<Directory /var/www/mydomain-www/public>
<IfModule rewrite_module>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [B,NE,QSA,L]
</IfModule>
</Directory>
<FilesMatch "(?i)\.php$">
Require all denied
</FilesMatch>
<FilesMatch "index\.php$">
Require all granted
</FilesMatch>
</VirtualHost>
You can move ws folder to /var/www/mydomain-www directory or create a symbolic link in the new directory. Then you need to change rule in /var/www/mydomain-www to skip anything with /ws/ in front:
<Directory /var/www/mydomain-www>
AuthType None
AllowOverride all
Require all granted
<IfModule rewrite_module>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ^/?((?!ws2/|ws3/).*)$ public/$1 [B,NE,L,NC]
</IfModule>
</Directory>

remove /public/index.php from Laravel url

i am very new to laravel. i've tried following solution to remove '.public/index.php' from Laravel url.
i have one htaccess file on root and other in public folder.
in root file i've written
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
and in public folder's htaccess file i wrote
<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>
but nothing worked.
still it opens list of directories in laravel
all i want is to convert my url www.abc.com/public/index.php into www.abc.com/ without removing files from public folder to some other folder type solution.
You simply need to fix your Document root and the Directory override rule within your sites .conf file.
On Ubuntu you will find this in /etc/apache2/sites-available
Your file should look something like this:
<VirtualHost *:80>
ServerName xxx.com
ServerAlias www.xxx.com
ServerAdmin xxx#xxx.com
DocumentRoot /var/www/html/laravel/public/ //<<< add public to your document root
<Directory "/var/www/html/laravel/public"> ## <<< add the override rule if not exists
AllowOverride all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Your laravel app should not be in the document root in any case, as you would allow anybody to access it!
SIDENOTE:
If you are using plesk or c-panel, you can edit the document root within the domain settings.
In your Apache configuration, set the document root to /yourapp/public -- this is meant to be the root web accessible folder; you're opening yourself up to access you may not want if the webroot is anything other than Laravel's public folder.

Defining Document Root in Apache2/Cake PhP

I have a broken Cake PHP site that I did not write, but am charged with fixing. I have the main controller up, however no css/js/images, and when I click a link I get a 404 not found. I believe something is incorrect with mod_rewrite or the docroot config in apache.
While reading through Cake documentation, I came across this:
"app/webroot
In a production setup, this folder should serve as the document root for your application. Folders here also serve as holding places for CSS stylesheets, images, and JavaScript files."
In my /etc/apache2/sites-enabled/this-site, I see this:
DocumentRoot /u02/data/docroots/this_site
<Directory /u02/data/docroots/this_site>
Options -Indexes
AllowOverride none
Order deny,allow
Allow from all
</Directory>
So, my question:
does the above config block need to have:
/u02/data/docroots/this_site/app/webroot as the DocumentRoot and ?
Anywhere else you can think to look for troubleshooting this?
Since you have access to edit the apache config files - it's most appropriate to make a standard production install.
Fixing the docroot
does the above config block need to have: .../app/webroot as the DocumentRoot
Assuming that path exists: yes.
This will fix the broken css/js/images.
Fixing mod rewrite
Put the rewrite rules in the virtual host config:
DocumentRoot /u02/data/docroots/this_site/app/webroot
<Directory /u02/data/docroots/this_site/app/webroot>
Options -Indexes
AllowOverride none
Order deny,allow
Allow from all
// added
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</Directory>
This will then route requests for anything that's not a file to the application.
Verify that the rewrite rule used is compatible with the version of CakePHP in use, the above is based on the rule for the latest release - it changed over the years and using the wrong rewrite rule may have unexpected side effects.
What's actually broken
AllowOverride none
This prevents the .htaccess files from being read by apache. If you were to change this to AllowOverride all, and assuming the currently-ignored .htaccess files exist, the site would work - but as a development install.
Try this:
<VirtualHost *:80>
ServerAdmin xxxxxx#test.ro
ServerName test.ro
ServerAlias www.test.ro
DirectoryIndex index.php
DocumentRoot /u02/data/docroots/this_site
ErrorLog /u02/data/docroots/this_site/error.log
CustomLog /u02/data/docroots/this_site/access.log combined
<Directory "/u02/data/docroots/this_site">
Options -Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
And you should have 3 .htaccess files:
/u02/data/docroots/this_site/app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
/u02/data/docroots/this_site/app/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/u02/data/docroots/this_site/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Hope this helps ;)

Categories