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]
Related
I cannot start my laravel project on cpanel server. Asked domain provider website to add the pyp version that my laravel project use but still have that problem. I need to solve this is 4 hours please help.
Here is my .htaccess
'''
Options -MultiViews -Indexes
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]
'''
If you're using cPanel on shared hosting, try the following assuming the root of your project is public_html (you can adjust the path if your project is in another directory within public_html):
Set the document root to the root of the project ie. /public_html
Create an .htaccess file in the root of the project that looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
This will direct requests to the public folder.
Create another .htaccess file in the public directory that looks like:
<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>
Are you getting the issue while accessing the web ?
Step 1: just upload your all source code on the server (make sure database & .env set well) i) make zip and extract it ii) deploy code using git or take a clone from a repository and install all dependencies
Step 2: Make a .htaccess file on the project root directory and copy below code and paste it into this file.
<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
I get this answer from this video so please say thanks to him https://www.youtube.com/watch?v=6Qbd9HTh7AE
Ref: How do I upload a laravel project on cPanel shared hosting?
or Try with a default Laravel and then upload your code
or create laravel project via Cpanel UI and try. Later replace with your custom code. Capture the key and .env files before replacing.
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
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>
I installed laravel framework in root folder, and wordpress CMS in Blog folder
Laravel Works well, But WordPress does not work
I redirect no-www to www and http to https in laravel .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 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}]
mysite url: https://www.zanborasal.com
wordpress blog folder : https://www.zanborasal.com/blog
Do you have any idea how can I correct my Wordp=Press installation?
Just after RewriteEngine On if you add:
RewriteCond $1 !^(wordpress-folder)
That will exclude your Wordpress install and let you have your own htaccess settings within the wordpress-folder.
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