Apache Mod Rewrite For Laravel - php

I have an installation of Laravel on Wampserver. The directory is as follows:
C:\wamp\www\laravel
Now URLs are like this:
http://localhost/laravel/public/index.php/home/index
So I used the following htaccess code
Options +FollowSymLinks
Options -indexes
DirectoryIndex index.PHP
RewriteEngine on
RewriteCond $1 !^(index\.PHP|images|robots.txt)
RewriteCond %{REQUEST_ FILENAME} !-f
RewriteCond %{REQUEST_ FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L, QSA]
To reduce the URL to
http://localhost/laravel/public/home/index
But the laravel framework insists that all application files reside in the public folder.
So I would like to know what I need to add to (or subtract from) the htaccess file so that the URL can look like
http://localhost/laravel/home/index
Thanks

When testing locally I do one of two things.
Create a new .htaccess below the public directory with the following.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Create a new virtual host. With WAMP you can navigate to C:\wamp\bin\apache\YOUR APACHE VERSION\conf\extra and find your httpd-vhosts.conf file, in there you can see example virtual hosts. Here's one of mine:
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias www.laravel.dev
</VirtualHost>
Make sure that your vhosts configuration file is being included. Open up your httpd.conf file and search for the vhosts file, uncomment the include line if it's commented out. Then I open the CLI and enter notepad "C:\windows\system32\drivers\etc\hosts" which opens up your hosts file. Underneath the item that mentions localhost place your new host. Here's an example.
127.0.0.1 laravel.dev
Make sure you restart Apache and bingo, you should be able to navigate to http://laravel.dev and you won't have any annoying public directory. This is how I achieve it, as I prefer the nicer looking virtual host rather then a long winded localhost URL.
Hope this helps.

I finally figured a way out. First of all, I had to open and edit my Apache httpd.conf by selecting it from the Wamp Aestran tray menu. The I had to uncomment the line
#Include conf/extra/httpd-vhosts.conf
After that, I opened the file which is located at the
<wampdirectory>/bin/apache/apache.x.y.z/conf/extra/httpd-vhosts.conf
then I added the following lines.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:/wamp/www"
ServerName localhost
Options Indexes FollowSymLinks
<Directory "C:/wamp/www">
AllowOverride All
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
#If you want to allow access from your internal network
# For specific ip addresses add one line per ip address
#Allow from 192.168.0.100
# For every ip in the subnet, just use the first 3 numbers of the subnet
#Allow from 192.168.0
</Directory>
</VirtualHost>
## must be first so the the wamp menu page loads when you use just localhost as the domain name
<VirtualHost *:80>
DocumentRoot "C:/wamp/sites/laravel/public"
ServerName laravel.dev
Options Indexes FollowSymLinks
<Directory "C:/wamp/sites/laravel/public">
AllowOverride All
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
#If you want to allow access from your internal network
# For specific ip addresses add one line per ip address
#Allow from 192.168.0.100
# For every ip in the subnet, just use the first 3 numbers of the subnet
#Allow from 192.168.0
</Directory>
</VirtualHost>
The next step was to edit my hosts file at C:\windows\system32\drivers\etc
and added
127.0.0.1 laravel.dev
Then restarted Wamp and it worked. Thanks to you guys for pointing me in the right direction. Really Appreciate it

The easiest way I got this working on my local dev environment was to do the following:
(Assuming you have WAMP installed in C:\WAMP)
Create the following folder:
c:\wamp\www\laravel
Download laravel and put the contents in the above directory. You will know you have done it right if you can browse to hxxp://localhost/laravel/public and get the opening screen. However, this isn't good enough. We want to get that screen by going to http://localhost/laravel
So then we do the following:
Create a textfile containing the following:
Alias /laravel "c:/wamp/www/laravel/public"
<Directory "c:/wamp/www/laravel/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Allow from all
</Directory>
Save this file as laravel.conf in the c:\wamp\alias directory.
Finally, restart your wampserver.
You should now be able to surf to http://localhost/laravel
Note that the above is strictly for a local development environment.

You're gonna end up with your code and your public folder residing in the same place, which most people do not recommend. I'd suggest you take advantage of using a local web server.
Why not make mysite.dev point to laravel/public directory so you could just use http://mysite.dev everytime, you have cleaner and shorter URL's too?

As a newb to WAMP and Laravel, I struggled at bit but did get the virtualhost thing to work on my WIN7PRO 64-bit box. In WAMPSERVER/Apache/hppd.conf at the end of the file, I added:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot C:/webapp/public
ServerName webapp
<Directory C:/webapp/public >
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from All
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot C:/wamp/www
ServerName localhost
</VirtualHost>
and I added:
127.0.0.1 webapp
to the hosts file. (I was never successful editing the vhosts files, as many posts on the web suggested.)
These changes allow me to get to my Laravel test app in my browser via
http://webapp
(and also, via just http://127.0.0.1)
and, to get to all my other sites, via:
http://localhost/devsite/whatever..

Related

XAMPP Access Forbidden

I have just downloaded the latest version of XAMPP with PHP version 7.2.4. I have made a very simple PHP validation for a HTML form and when I press submit it comes up with the following:
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server.
If you think this is a server error, please contact the webmaster.
Error 403
localhost
Apache/2.4.33 (Win32) OpenSSL/1.1.0g PHP/7.2.4
I'd don't know what the problem is as I have tried changing Require none to Require all granted.
Please Help!
I have experienced this problem and found out that localhost link is not configured in
httpd_vhosts.conf.
So I added this line at the bottom part of httpd_vhosts.conf
<VirtualHost *:80>
DocumentRoot "E:/xampp/htdocs"
ServerName localhost
</VirtualHost>
Well, probably this must be happening because the localhost link is not configured in your xamp vhost, try to look for the vhosts configuration file and add the same one there. Just add this block of code making the appropriate path changes until your repository so that you can access the localhost:
# Virtual Hosts
#
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
DocumentRoot "${INSTALL_DIR}/www"
<Directory "${INSTALL_DIR}/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#hcode.com.br
DocumentRoot "C:\ecommerce"
ServerName www.hcodecommerce.com.br
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
<Directory "C:\ecommerce">
Require all granted
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</Directory>
</VirtualHost>
By default in httpd.conf your entire system directory "/" is secured and not allowed access
in httpd.conf:
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride none
Require all denied
</Directory>
Add the below additional under the above
<Directory /home>
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
Change /home to your hosted installation public directory - e.g. /projects or /devsite.local etc...
For more info on Apache see here: https://httpd.apache.org/docs/current/mod/core.html#directory
The 'Options' are typical .htaccess directives - change as needed
The 'AllowOverride All' gives access to the /home folder and everything under it
The 'Require local' makes sure only localhost / 127.0.0.1 has access to folder and files under /home
Hope this helps :D

Laravel Name resolution issue

I am new to laravel and trying to learn laravel but I am stuck while
entering a server name in httpd-vhosts.conf I entered this in httpd-vhost.contf:
<VirtualHost *:80>
DocumentRoot "D:/Xampp/htdocs/"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/Xampp/htdocs/lsapp/public"
ServerName lsapp.dev
</VirtualHost>
and in system32 host file :
127.0.0.1 localhost
127.0.0.1 lsapp.dev
See the error in homepage
Use run this command, in project root
php artisan serve --host=lsapp.dev --port=80
Be sure to stop mysql from xampp , which uses port 80,
Above command require to set 127.0.0.1 lsapp.dev in your host file, as you did already
If you want to go without command set DocumentRoot in httpd-vhost.contf as you did already
"D:/Xampp/htdocs/lsapp/public"
ServerName lsapp.dev
And in host file
127.0.0.1 lsapp.dev
And restart apache, stop and then start apache to reflect, and hit lsapp.dev browser
<VirtualHost lsapp.dev:80>
DocumentRoot "D:/Xampp/htdocs/lsapp/public"
ServerAdmin laravel.dev
<Directory "D:/Xampp/htdocs/lsapp/">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
and don't forget the .htaccess file in public folder
Apache (from laravel docu...)
The framework ships with a public/.htaccess file that is used to allow URLs without index.php. If you use Apache to serve your Laravel application, be sure to enable the mod_rewrite module.
If the .htaccess file that ships with Laravel does not work with your Apache installation, try this one:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
If your web host doesn't allow the FollowSymlinks option, try replacing it with Options +SymLinksIfOwnerMatch.

How to perfectly set up virtual host for codeigniter project?

I am trying to make a virtual host for a codeigniter project. I have done this in httpd-vhosts.conf:
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\CI_projects\facebook-login"
ServerName dev.facebook-login.com
<Directory "C:\xampp\htdocs\CI_projects\facebook-login">
Require all granted
</Directory>
</VirtualHost>
and in application/config/config.php,
$config['base_url'] = 'http://dev.facebook-login.com';
and
$config['index_page'] = '';
the browser opens the landing page. but when transiting from any other uri it says object not found.
And when i configure httpd-vhosts.conf like this:
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\CI_projects\facebook-login\index.php"
ServerName dev.facebook-login.com
<Directory "C:\xampp\htdocs\CI_projects\facebook-login\index.php">
Require all granted
</Directory>
</VirtualHost>
It arises problem with assets things, i,e images and some css doesnt loads. How can I solve it?
I am on windows 10 and I use xampp with virtual host this is way I set up.
Put forward slash at end of base url
$config['base_url'] = 'http://dev.facebook-login.com/';
First go to
Windows > System32 > drivers > etc > host
You may need to open as administrator to be able to save it
You might see some thing like
127.0.0.1 localhost
Below that create another one for dev.facebook-login.com like example:
127.0.0.1 localhost
127.0.0.1 dev.facebook-login.com
Save it
Then go to the
xampp > apache > conf > extra > httpd-vhosts.conf
open it as administrator so you can save it.
No need for the index.php on DocumentRoot
<VirtualHost *:80>
##ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/xampp/htdocs/CI_projects/facebook-login"
ServerName dev.facebook-login.com
##ServerAlias www.dummy-host.example.com
##ErrorLog "logs/dummy-host.example.com-error.log"
##CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
Save it and restart the severs.
I use this for my htaccess
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
httpd-vhosts.conf
Listen 1122
<VirtualHost *:1122>
DocumentRoot "D:\laragon_server\www\hugpong_production"
<Directory "D:\laragon_server\www\hugpong_production">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
https://www.namecheap.com/support/knowledgebase/article.aspx/10026/33/installing-an-ssl-certificate-on-xampp/
php 8 is not ready for CI4 as of jan 2021!
XAMPP has preloaded ssl certificate in php 7 packages
consider uncommon vhosts extra and place this in it
my only persistent issues is codeigniter 4 routes, have to turn on php spark serve to get XAMPP to route pages outside of default index, nothing else works, its garbage to that extent, will try with composer next....a whole book needs
to be wrote about codeigniter 4 routes and why they don't work, 200 pages plus
<VirtualHost localhost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
<VirtualHost example.com *:8080>
ServerName example.com
ServerAlias www.example.com
DocumentRoot "C:\xampp\htdocs\codeignite4\public"
//my project folder is codeignite4 without using composer this time
//i added listen 8080 under listen 80 in htppd.conf
ServerName example.com
<VirtualHost truservex.com *:443>
DocumentRoot "C:\xampp\htdocs\codeignite4\public"
ServerName example.com
SSLEngine On
SSLCertificateFile "C:/xampp/apache/conf/ssl.crt/server.crt"
// you can verifiy these file locations, certificate data
SSLCertificateKeyFile "C:/xampp/apache/conf/ssl.key/server.key"
// going without the Directory tag can be helpful to drop in vhosts
<Directory "C:\xampp\htdocs\codeignite4\public">
Require all granted
doing this i was able to replicate the original install, create site access
using http://localhost:8080 or http://example.com/ as my base url
in my example index.php is removed, set to ''
when using command prompt to get into codeignite4 project folder i can
launch php spark serve and routes will work that were previously 404
Using Xampp 7.3.25 Dec 2020 because php 8 will not run error
free with codeigniter 4 as of this date, this worked for me as
my first codeigniter 4 virtual host setup, my install had
difficulty initially with using port 80, seems to want to for
port 443 if it can..I'm not a server pro but i wanted to
leave this for the next guy....This is strictly name based
virtual hosts for develop, ip based would have obvious
advantages. Because i had trouble in initial CI4 testing with
port 80 the (*) in virtual hosts vs (*:80) seemed to solve one
hurdle in searching for more ports, now i can finally begin my
first project ...my default URL was also changed to example.com
in appstarter/apps/config file. Carefully reread <tags> for
typos, its easy to shut server down!
<VirtualHost *>
DocumentRoot "C:/Xampp/htdocs/"
ServerName localhost
<Directory "C:/Xampp/htdocs/">
Require all granted
<VirtualHost *>
DocumentRoot "C:/Xampp/htdocs/appstarter/public/"
ServerName example.com
ServerAlias www.example.com
# maybe don't attempt to use dir tag, shuts down server
# the directives at httpd.conf appear to suffice
I have added a htaccess file in the appstarter/public
I assume this is primitive, can get far more advanced
The Apache help pages helped me focus a bit more on
what i must accomplish, name based virtual hosts this case
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
2.2 Apache configuration: :Apache server context important!
Order allow,deny :The hackers are winning.....
Allow from all
2.4 Apache configuration:
Require all granted

Unable to access routes without index.php in Laravel 4

Ok, so I have spent a considerable amount of time googling for suggestions for this, and despite setting up several test project in Laravel 4 before on WAMPserver, I am not sure what I am missing in this case. Hopefully I have provided all details necessary on my setup and what I have tried so far.
WAMPserver version 2.4, located in C:/wamp/
Apache version 2.4.4
PHP version 5.4.12
mod_rewrite enabled (I can see it in phpinfo())
Project located in C:/wamp/www/wsrp/
in C:/wamp/www/wsrp/public I have the following .htaccess file:
<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>
In my C:/wamp/bin/apache/Apache2.4.4/conf/httpd.conf file I have uncommented this line:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
In my C:/wamp/bin/apache/Apache2.4.4/conf/extra/httpd-vhosts.conf file I have the following:
<VirtualHost *:80>
DocumentRoot "C:\wamp\www"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/wsrp/public"
ServerName localhost
Options Indexes FollowSymLinks
<Directory "C:/wamp/www/wsrp/public">
AllowOverride All
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
</VirtualHost>
In my C:/wamp/www/wsrp/app/routes.php file I have this test route:
Route::get('test', function() {
echo 'Hello world!';
});
To test I am trying to visit this URL:
http://localhost/wsrp/test
And this doesn't work, it shows me an Internal Server Error page. If however I visit this URL:
http://localhost/wsrp/index.php/test
I see my 'Hello world!' message displayed.
Obviously I want to be able to work on this project without having the index.php in the URL, that is the problem.
Of all the SO questions/answers I have looked at, they have all suggested adding a virtual host, which I've done, having the right .htaccess file in C:/wamp/www/wsrp/public/ which I've done, having mod_rewrite enabled, which I've done and yeah.. not sure what else to try, or what I am missing. Help and suggestions greatly appreciated!
Edit:
Forgot to mention that I also have an alias file in C:/wamp/alias/wsrp.conf
Alias /wsrp "c:/wamp/www/wsrp/public"
<Directory "c:/wamp/www/wsrp/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Allow from all
</Directory>
Firstly why do you have a localhost v-host? The root directory for WAMP servers is typically c:/wamp/www that is already mapped by HOSTS to 127.0.0.1 or localhost. That v-host is irrelevant.
Secondly, as your c:/wamp/www directory is being used to contain your sites, httpd.conf already contains the <Directory> details it needs, from memory it looks something like:
<Directory c:/wamp/www>
Options Indexes FollowSymLinks
AllowOverride All
Require local
</Directory>
You don't need to define any special directory information for sub directories unless you want to add specific module configuration or change any of the defaults. To get your vhost working you need add no more than:
<VirtualHost *:80>
ServerName mylocalsite.dev
DocumentRoot c:/wamp/www/mysite/public
</VirtualHost>
I would suggest however that you put in locations for error logging / access logging.
I've had the same problem that you. I solved this problem in this way.
Open C:\wamp\bin\apache\Apache2.4.4\conf/httpd.conf
Then, You may search the next line, #LoadModule rewrite_module modules/mod_rewrite.so
Remove the pound ('#') sign at the start and save the file.
Next, restart your WampServer and test.
I hope help you!

phpmyadmin working when i turn off certain vhosts

Pretty confused right now, while i feel i know a bit about vhosts this has confused me for no end of time and i thought i'll seek help after so long :p
I turn off 1 vhost and phpmyadmin will work locally, turn this vhost back on, phpmyadmin ends up at this webpage with my 404 for the site. the vhost in question:
<VirtualHost 127.0.0.1>
DocumentRoot /var/www/mayfieldafc/docroot
ServerName mayfieldafc.com
ServerAlias mayfieldafc.local
#CustomLog logs/mayfieldafc-transfer.log combined
#ErrorLog logs/mayfieldafc-error.log
php_value include_path "/usr/share/php:/var/www/mayfieldafc"
php_value error_reporting 2047
php_flag magic_quotes_gpc off
php_flag short_open_tag off
DirectoryIndex index.php index.html
<Directory /var/www/mayfieldafc/docroot>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
#RewriteLogLevel 9
#RewriteLog /var/log/mod_rewrite.log
# Dispatcher wiring
RewriteCond %{REQUEST_URI} !dispatch\.php$
RewriteCond /var/www/mayfieldafc/docroot%{REQUEST_FILENAME} !-f
RewriteRule ^(/.*)$ /dispatch.php?url=$1 [L,QSA]
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot /var/www/mayfieldafc/docroot
ServerName assets.mayfieldafc.com
ServerAlias assets-mayfieldafc.local
#CustomLog logs/mayfieldafc-transfer.log combined
#ErrorLog logs/mayfieldafc-error.log
DirectoryIndex index.php index.html
<Directory /var/www/mayfieldafc/docroot>
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
If anybody knows anything about vhosts and phpmyadmin i'd love you forever :D
Using NameVirtualHost to host multiple sites on one IP address requires that you access the VirtualHost at its name and not its IP address. Hence, if phpmyadmin is hosted on assets-mayfieldafc.local, you must access it at assets-mayfieldafc.local/phpmyadmin. It works at localhost when only one virtualhost is configured as it only has one place to go for the localhost IP. Configure more virtualhosts on the same IP, and you will need to access them by ServerName/ServerAlias.
In the end nothing i could change in my vhost settings was working so i went with the obvious answer.
I just created a separate vhost with a
servername phpmyadmin.local
and just pointing the document to myphpadmin on my hard drive and all was good.

Categories