I install laravel app in root of my shared host "public_html now I want to install Russian version of this app in ru/ subfolder but when I go to example.com/ru I got 404 Page not found error. I use apache web server my .htaccess file in root folder contain these code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
how should i change this configuration?
Thanks.
You will do it in the same way you installed it in the root directory. I will tell you how I installed it on my shared hosting account, both main and sub domains. After I uploaded all my project to the subfolder, and your will be example.com/ru, do the following:
In your public folder there is your .htaccess file. since you are basically making this a subdomain, it will need to be in the root of that subdomain, so transfer the .htaccess from public folder to the root of ru folder.
Open the .htaccess and change/add the following:
DirectoryIndex public/index.php
and in the RewriteRule change it to this:
public/index.php
And just to be clear, your .htaccess should be like this at the end after your changes:
DirectoryIndex public/index.php
<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 ^ public/index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Create a .htaccess file in a root folder and paste the code and save it.
<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
Related
When filling on hosting, I faced the problem that you can not select the start folder of the opening. When opening a link юhttps://my-domen/ this opens
to get to the site, enter /public
how to make sure that when you open the site, the public folder is immediately opened on the hosting so you can not configure it . Can I use htaccess or something else? If this helps then the laravel files are a clean project
htaccess
<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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
In your root folder use this .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
In the public folder use the new clean laravel .htaccess file
How can I remove /public/ from url and auto force http to https?
my root .htaccess (I created it)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTPS} !on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
public/.htaccess (by default laravel)
<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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
But it not works - no auto http to https redirect.
So I neet to combine removing public folder from url and forcing http to https using .htaccess
The problem you're having is that you've set your document root to be the root of the project. In your apache virtual host config you need to set the document root to be the public directory.
For example:
DocumentRoot /var/www/yourproject/public
That way, public doesn't appear in the URL.
If you're using shared hosting you'll want to move the root of your project to above public_html/, delete public_html, then rename your public directory to public_html. You can then go and modify the bootstrap files to use this path. You could also symlink it.
Add this two line into your Public Folder htaccess file
RewriteCond %{HTTPS} !=on
RewriteRule .* https://yourdomain.com%{REQUEST_URI} [R,L]
Full Code of your public/.htaccess
<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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://yourdomain.com%{REQUEST_URI} [R,L]
</IfModule>
I think you need to enable rewrite_url with
$ sudo a2enmod rewrite
and
restart your apache server
$ sudo systemctl restart apache2
Your site-enabled/000-default.conf
DocumentRoot /var/www/html/yourapp/public
May be you can use
RewriteBase "yourapp/public/"
or refer this answer.
Rename server.php in your Laravel root folder to index.php
Copy the .htaccess file from /public directory to your Laravel root folder.
Put this code in .htaccess file of root directory of domain for removing 'public :
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
And for redirecting to https from http add following lines at the top in .htaccess file located in public directory of laravel project.
RewriteCond %{HTTP_HOST} your_domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.your_domain.com/$1 [R,L]
I am using Laravel 5.2 in my shared hosting server. I would like to remove public from url without changing server.php to index.php in root directory as doing so .env and other files are publicly visible. My .htaccess look likes below:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,NC]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
RewriteRule ^(css|js|images|media)/(.*)$ /public/$1/$2 [L,NC]
</IfModule>
You need to point you domain to public folder from cpanel.
or
Move your .htaccess file from the /public directory to the root directory and replace it content with the following code:
<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>
Create new
.htaccess file in root directory
and paste with the following code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
AddType application/x-httpd-ea-php56___lsphp .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
The Best Solution for this will be pointing your apache document root to laravel's public folder.
But as you said you are using shared hosting then that might not be possible for you.
Now to access laravel app without public in the url follow this question
Laravel 5 - Remove public from URL
Also, you are concerned about other folders like app, config etc to not be accessible from the browser. Therefore to do that you can place an .htaccess
file with contents Deny from all in every folder that you want to block access from the browser.
This is the exact working solution i found. Solution is taken from here, answered by Pallav Nagar
Create .htaccess file in root directory and place code something like below.
<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>
In root folder make .htaccess file with:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I have a problem when uploading a new laravel 5.5 project. When I alter the htaccess in the public_html folder on my server it keeps redirecting. However this problem wasnt there on laravel 5.0. Here's my htaccess from the public_html folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
And here's the htaccess in the public folder:
<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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Any suggestions on how to solve this problem?
Beside the fact that it is really not good idea to put a Laravel project in your Document Root, you have a problem with your htaccess file in your Root directory.
Additional I would at least protect all files in your root folder from direct access:
<IfModule mod_rewrite.c>
RewriteEngine On
# Fake 404 for all files in /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?[^/]+$ - [R=404,L]
# Protect rewrite loop: exclude /public
RewriteCond %{REQUEST_URI} !^/?public(/.*)?$
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
My project (Silex) has a front controller web/index.php and assets in web/css/*, web/img/* and so on. I've placed the following .htaccess file in the root of the public html folder:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Every request should be rewrited to web folder (RewriteBase web directive) and if file doesn't exist (!-f) it should be routed to the front controller.
This is not working:
<img src="/img/myimage.png" alt="" />
File myimage.png exists, but request for /img/myimage.png gives me a 404 folder. If I modify the path to /web/img/mmyimage.png it works.
Is there something I'm missing?
You could try routing the css and img folders explicitly:
RewriteCond %{DOCUMENT_ROOT}/web%{REQUEST_URI} -f
RewriteRule ^(css|img) /web%{REQUEST_URI} [L]
You'd need to add that right before the condition for the index.php routing rule.
Replace your .htaccess with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
# if requested files exists in /web then forward it there
RewriteCond %{DOCUMENT_ROOT}/web/$1 -f
RewriteRule ^(.+?)/?$ /web/$1 [L]
# if requested files doesn't exist in /web then forward it to index.php
RewriteCond %{DOCUMENT_ROOT}/web/$1 !-f
RewriteRule ^(.+?)/?$ /web/index.php [L]
if your server is running Apache >= 2.2.16 you can use:
FallbackResource /index.php
in your .htaccess instead of
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
See also Apache’s fallbackresource: your new .htaccess command for more information.
You need to change web to /web as Kyra mentioned.
Options -MultiViews
RewriteEngine On
RewriteBase /web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
But that's not going to solve your issue.
Main problem is your DocumentRoot is pointing to /path/to/project and it should point to /path/to/project/web
I managed to fix it with these lines:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/web/$1 -f
RewriteRule ^(.+?)/?$ /web/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ web/index.php [QSA,L]
</IfModule>