Laravel get rid of /public path in wamp - php

I'm using Laravel on WAMP server.
I have a hard time to get rid of /public/ in the URL path.
I found many workarounds on the web but no solution is working for me. I changed my .htaccess, my httpd.conf,... tried to make a symbolic link through windows console, but still nothing working.
Here's my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost:8080/blog$ [NC,OR]
RewriteCond %{HTTP_HOST} ^localhost:8080/blog$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Hereafter my httpd.conf
# Alias for Laravel public directory
Alias /Lara/ "C:/Bitnami/wampstack-5.4.31-0/apache2/htdocs/blog/public/"
<Directory "C:/Bitnami/wampstack-5.4.31-0/apache2/htdocs/blog/public/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Related

Laravel Hosted in Subdirectory

A client has requested a website built and now that it has been built, they've requested it be hosted alongside their existing website but in a subdirectory (example.com/example/path).
What I'm dealing with now is figuring out the required apache rules to host this correctly. I've uploaded the entire project structure to that directory. Within the public directory (/example/path/public) I have the following .htaccess file (henceforth I'll refer to this as the Laravel .htaccess):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /example/path
# 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]
# Serve Cached Page If Available...
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{DOCUMENT_ROOT}/page-cache/pc__index__pc.html -f
RewriteRule .? page-cache/pc__index__pc.html [L]
RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.html -f
RewriteRule . page-cache%{REQUEST_URI}.html [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
<FilesMatch ".(jpg|jpeg|png|gif|ico|svg|woff|woff2|ttf|eot|otf)$">
Header set Cache-Control "max-age=2628000, public"
</FilesMatch>
<FilesMatch ".(css|js)$">
Header set Cache-Control "max-age=86400, public"
</FilesMatch>
I have added the RewriteBase line based on me trying to find a solution to this.
Within the base .htaccess file (/example/path/.htaccess), I have the following:
<IfModule mod_alias.c>
Alias /example/path /home/www/<user>/html/example/path
<Directory "/home/www/<user>/html/example/path">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</IfModule>
The problem is however that this returns an internal server error. I've tried other potential solutions that don't use Alias that instead return 40X errors.
I need to be able to have the Laravel site fully working when a user visits example.com/example/path. I've tried numerous solutions over the past 90 minutes and none have worked.
What is the correct solution to my situation?
In public folder we have hosted 8 platforms and each platform's root directory we have this .htaccess configuration. hope this helps you.
<ifmodule mod_rewrite.c>
<ifmodule mod_negotiation.c>
Options -MultiViews
</ifmodule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</ifmodule>
I did this recently too. My client had their Wordpress set to resolve at as the Document root (/var/www/html), but they also wanted their Laravel app "installed" in a subfolder of Wordpress and named "hq"
This however would be a security problem, because we only want Laravel's Public folder exposed, and not the application files. To solve this problem, I used the power of symbolic links!
The Laravel app was placed below the Document root at:
/var/www/laravel
I then exposed Laravel's public folder in the DocumentRoot with the command "ln -s symlinkLocation symbolic-link-name"
ln -s /var/www/laravel/public /var/www/html/hq
In the above line, you can see that "hq" is not an actual folder, but instead a symlink to /var/www/laravel/public
(which is where Laravel's index.php entry point is, the css, js etc)
Here is what the symbolic link looks like:
I also made a symbolic link to the storage folder:
ln -s /var/www/laravel/public/ /var/www/laravel/storage/app/public
Which accomplishes what "php artisan storage:link" accomplishes.
For reference, here is what my laravel .htaccess looks like:
The .htaccess in /var/www/laravel/public/.htaccess was
<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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Hope this helps
create a htaccess file in root directory of project
and add the following lines :
#disable directory browsing
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
#PROTECT ENV FILE
<Files .env>
order allow,deny
Deny from all
</Files>
#PROTECT htaccess FILE
<Files .htaccess>
order allow,deny
Deny from all
</Files>

Apache2.4 & Laravel 5.4 with alias in a subfolder doesn't work without /index.php

I've created an alias on apache server conf file (/etc/apache2/mods-enabled/alias.conf) to target the public folder.
Alias /laravel/ "/home/user/dev/web/laravel/public/"
<Directory "/home/user/dev/web/laravel/public/">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
Require all granted
</Directory>
Also, I wrote the following .htaccess
<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 ^/laravel/(.*) /laravel/index.php/$1 [L]
</IfModule>
Now, when I'm visitng the http://IP/laravel the index.php is loaded as expected. But if I'm gonna make a call to a GET Route like http://IP/laravel/load_examples I'm getting back a NOT FOUND error.
Instead, if I visit the http://IP/laravel/index.php/load_examples, route loaded as expected.
It seems to me that something's going wrong with the .htaccess and rewrite rules.
I tried everything I found on the SO and other places but couldn't find any solution so far.
Any idea?
In the alias before the the code block shown try adding:
DocumentRoot /home/user/dev/web/laravel/public/
And then try this for your .htaccess:
<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>

Laravel 5.3 Route NotFoundHttpException

I have deployed my Laravel 5.3 application on an AWS server running Ubuntu and php 7. i have created an alias in the /etc/apache2/sites-available/000-default.conf file as below:
Alias /myapp.dev /var/www/html/myapp/public/
<Directory "/var/www/html/myapp/public/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
RewriteEngine On
RewriteBase /var/www/html/myapp/public/
</Directory>
But when i try to access with the alias as 52.xxx.xxx.xx/myapp.dev i get the following error:
How ever when i access it with full url to the public/ directory it works fine
eg. 52.xxx.xxx.xx/myapp.dev/public/login
I have enabled a2enmod and my .htaccess file in /public directory is as below
<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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
any ideas as to why i am getting the Route NotFoundHttpException in my aws server and not to forget to mention that in my local windows machine running php 5.6 also is running fine with a vhost. Your ideas are very much appreciated. Thanks in advance
Try this .htaccess. Hope it will help.
<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>

Move all .htacces configurations in cakephp to Apache virtual host

My intention is to speed up the performance of the cake app. I copied all the .htaccess files from my cake app, and despite the configtest saying all is ok, I still get an internal server error when I try and load the page. Is this the correct way to bring in .htaccess files from the app directory?
<Directory /var/www/html/aga-stag>
Options Includes FollowSymLinks
Require all granted
AllowOverride None
Order deny,allow
Allow from all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^kwiksta\.com [NC]
RewriteRule ^(.*)$ http://www.kwiksta.com/$1 [L,R=301]
RewriteRule ^(red5|oflaDemo) - [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
</Directory>
.htaccess files are valid apache conf syntax, so in principle all you need to do is copy and paste the htaccess file's contents into the right place. However don't blindly do that.
This is more appropriate for handling your kwiksta.com -> www.kwiksta.com redirects:
<VirtualHost *:80>
ServerName kwiksta.com
RewriteEngine On
RewriteRule ^ http://www.kwiksta.com{REQUEST_URI} [R=301,L]
</VirtualHost>
This snippet:
RewriteRule ^(red5|oflaDemo) - [L]
Is better handled by just putting (or symlinking) those folders (assuming that's what they are) into the webroot
And this is all that's required to then handle your CakePHP application:
<VirtualHost *:80>
DocumentRoot /var/www/html/aga-stag/app/webroot
ServerName www.kwiksta.com
AllowOverride None # No need for htaccess files now
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</VirtualHost>
Note that the document root points at the webroot folder as it should for a any and all production installs, which renders 2 of the 3 htaccess files irrelevant.
Be sure that the rewrite rules match the version of CakePHP you are using as they changed over time.

Installing codeigniter on subdomain but it is giving 500 internal server error

I have installed the codeigniter on subdomain like http://abc.domain.com.
I have modified my htaccess file but it is giving 500 internal error. Please
tell me required changes to be done in htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /abc
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Change the RewriteBase to / only and add base_url to your config file like,
# in htaccess change the RewriteBase
RewriteBase /
in application/config/config.php
$config['base_url'] = 'http://abc.domain.com/';
Also make sure that your htaccess file should be in your abc folder, not on the root www
Firstly I hope you have configured your virtual host properly.
For example:
<VirtualHost *:80>
ServerName abc.domain.com
DocumentRoot /var/www/html/abc/or/whatever/
</VirtualHost>
And your Directory directive
<Directory /var/www/html/abc/or/whatever/>
AllowOverride all
</Directory>
Then place this in the /var/www/html/abc/or/whatever/.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index.php|images|captcha|css|js|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
This should work in most cases. And don't forget to restart apache!

Categories