I have laravel 9 running locally on windows (MAMP) and I want to open the home page with:
localhost/myproject
instead of
localhost/myproject/public
I tried to add .htaccess to the root folder with the following:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
and it didn't work! (I got 404),
(Although this worked with laravel 8!)
so what is the solution for this !? I spent hours on searching blogs and tweaking to no avail!
I also read on some discussions that using .htaccess or replacing the server.php and renaming it are NOT totally safe for production.
so what is the solution, and would this solution also work if I host my website online (for example on shared hosting) ?
THANKS
I have deployed several websites to hosting and vps servers with following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
If error still occurs you may contact to your hosting provider and ask them to give you valid .htaccess rules. You may move your files inside public folder to root folder as well.
Please beware of doing solutions like htacces and changing laravel core files in production, which will make your private files public.
there is a more secure way to do it.
Developments
use: `php artisan serve`
production
you can use Document Root to make the domain point to your project's public folder.
To remove the public folder from laravel 9 you need to customize the .htaccess file.
You must have mod_rewrite enabled on your Apache server. The rewrite module is required to apply these settings. You also have enabled .htaccess in Apache virtual host for Laravel.
Update the code into your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
create a .htaccess in root directory
put these two line in it
RewriteCond %{REQUEST_URI} !^/public($|/)
RewriteRule ^(.*)$ public/$1 [L]
Related
We have two hosting packages at godaddy. Our live website is working fine using following .htaccess file. Website is accessible without using index.php in url.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
We used same htaccess file and same code on other hosting that is also by godaddy. We just wanted to make production and development instances seperate. But on other hosting with same code and htaccess file, website shows 404 page not found error but website works fine with index.php in url.
We have same php version on both server. Just wants to know what could be the issue?
I also faced the similar problem when I move my 100% working code to dedicated Debian server. After 3 hours of research, i found out why htaccess file is not working.You need to allow overriding of htaccess in Apache Configuration.
Here are the steps.
Step 1:
Add this in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Step 2:
Remove index.php in codeigniter config
$config['index_page'] = '';
Step 3:
Allow overriding htaccess in Apache Configuration (Command)
sudo nano /etc/apache2/apache2.conf
and edit the file & change to
AllowOverride All
for www folder
Step 4:
Restart Apache (Command)
sudo /etc/init.d/apache2 restart
Before you make any changes in apache2.conf file make sure to make a backup copy of that file. If you are using Debian server you can avoid sudo command. I believe you know how to connect to your server using ssh client software like putty. Comment below if this method does not work for you.
Follow this
Create a new .htaccess file in your public_html/Project root
Paste the following code
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|resources|robots\.txt|static) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?$1 [L,QSA]
Optional
If you can ssh into the server using command ssh username#ip_address on a command line either Linux terminal or Putty
The username is the one given to you by GoDaddy or you can use root
The ip_address would be the Public IP to your host you can find it in your hosting panel
Run the command sudo a2dissite 000-default
The command disables the default apache config that handles request
Goodluck!
You can use this. This works for me in godaddy hosting.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?/$1
try this one.. it's works for me..
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
I have faced the similar problem when I move on godaddy server.And It's work for me.
Add this code into your .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
In config file:
$config['base_url'] = 'domain.com';
$config['index.php'] = '';
And Set AllowOverride to All in your httpd.conf
set the correct permission on the /cache folder, rather than just making the folder writable you need to make it reclusively writable.
I hope it will work for you.
Try to lowercase all your controllers. This fix the problem for me.
in index.php you echo "test" and run .then you can check whether it is called or not.
in config file enable error log .
Make your /application/logs folder writable.
In /application/config/config.php set. $config['log_threshold'] = 4;
check the log /application/logs folder
I have limited knowledge in server networking but this should give you a few ideas.
First ,you must know what is .htacess file is. you can check it here link
.htaccess is a configuration file for use on web servers running the
Apache Web Server software. When a .htaccess file is placed in a
directory which is in turn 'loaded via the Apache Web Server', then
the .htaccess file is detected and executed by the Apache Web Server
software. These .htaccess files can be used to alter the configuration
of the Apache Web Server software to enable/disable additional
functionality and features that the Apache Web Server software has to
offer. These facilities include basic redirect functionality, for
instance if a 404 file not found error occurs, or for more advanced
functions such as content password protection or image hot link
prevention.
Basically .htaccess is giving you permission to edit the configuration of Apache Web Server. You will gain permission using .htacess if mod_rewrite is enabled in Apache. I will not write what that 4 basic line in .htaccess can do in codeigniter because you can find it in google. this link can give you some nice basic info.
Now the main issue is .htaccess is not working in your other hosting. This is possible because a few reason :
For some reason, mod_rewrite is disabled in your Apache Web Server. If your hosting is VPS or DS it's possible you are didn't configuring it yet. Someone already ask how to enable mod_rewrite, you can go to this link. If you are using Shared Hosting, ask your hosting to enable mod.rewrite in your host. You can check if mod.rewrite enabled or not in your hosting following this link. Or you can simply check with <?php phpinfo(); ?> and check in "Loaded Modules" for mod_rewrite exist or not. Example in this image.
You are not using Apache as Web Server. There are others Web Server as NGINX and lighttpd. You should ask your server administrator what Web Server are you use currently. I dont have any experience using NGINX and lighttpd so i cannot give you any help in that. But as far as i know .htaccess code is different in APACHE, NGINX, and lighttpd. You can try to convert .htaccess apache code to nginx code in here or here. For converting .htaccess to lighttpd unfortunately, i cannot find any online converter but there are official guide here and here
Well for the complete tips sake i will write this. It's possible you are not configuring codeigniter to not use index.php. In config file, empty configuration for index_page. and give Base_url your domain name. $config['base_url'] = "yourdomainname.com"; $config['index_page'] = '';
I hope this clear your confussion why your .htaccess didnt work.
As you mentioned here, on localhost your code is working fine but when you moved it over live server it causes 404 error. There might be few cases you need to check:
Step 1: Check mod_rewrite is enabled or not in your Apache configuration
Step 2: Remove index.php in codeigniter config as #geordy suggested
You can check it by
<?php
echo 'Test'; die;
?>
in your root index.php. It ensures your all requests should go through this files else Codeigniter framework would not work properly.
I currently use laravel 5 for web development. And my goal is to upload the website which I made in laravel to a shared host.
But if I upload my files to the public_html folder, I'll have to navigate to mywebsite.com/public/mypage to access a route mypage. Which is not what I want.
I would have just changed the the name of public folder to public_html and then shifted all other folders to the root directory and changed some laravel config options, but that would have made my root directory unclean. So think I would have to use .htaccess for what I want. I am not familiar in using .htaccess, so how would I do this:
If the user enters mywebsite.com/mypage in the browser he gets to see the page mywebsite.com/public/mypage in the browser, but the url in the browser stays the same.
Thanks, in advance.
I also got the same problem when hosting my website in shared hosting godaddy.
I also used laravel5 frameowork and launched the site http://www.hahafunnyjokes.com
Edit the .htaccess file in the root folder where your public_html pointing
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.examplecom/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Thats it, I got my site up and running.
Replace example.com with your website name
I've searched through Web to solve my problem but non of the solutions works for me there, so after couple of hours of struggling with Routing module I've decided to ask you for a helping hand.
Problem
I am unable to access Laravel application from outside /public directory. I need to type localhost\projects\laravel\public in my browser but what I want is to use the URL withour /public. The main reason is because I use shared hosting and have no access to apache configuration file so I'm unable to create vhost.
Background
I've installed manually Laravel framework under the: **c:\xampp\htdocs\projects\laravel** and used composer to do the rest for me.
The routing has been set to: Route::get('/', 'HomeController#showWelcome');
In my project root directory **c:\xampp\htdocs\projects\laravel** I've put a .htacces file with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projects/laravel/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
In my Application Root dir c:\xampp\htdocs\projects\laravel\public I have .htaccess with the following code in it:
<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>
When I type: localhost\projects\laravel\ I've got NotFoundHttpException
When I type: localhost\projects\laravel\public I've got welcome page
Any help would be appreciate, Thanks.
Finally I've got this thing working. There are two solutions that can be made.
One requires to move all -public content level-up
Second requires slightly changes in your shared hosting directory root structure - if allowed
Solution #1
The first that works for me that I didn't want to implement has been posted by #Wasim in this thread: Laravel 4 removing public from URL The solution is not save as the content core structure is in the same directory as application itself. This could cause some problems in future implementation.
You need to move all the content from public/ folder one level-up into project ROOT directory then replace internal paths in index.php file for correct onece. For security reasons this .htaccess file needs to be put into project ROOT directory
<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>
Modified Laravel structure described above can be uploaded to your shared web hosting and the application should runs withour /public in yor http address.
Solution 2 (with no access to http.conf file on your shared hosting)
This solution does not requires form you to move the content of the /public folder one level-up but requires form you to have read and write access to ../ROOT directory in your shared hosting (../ROOT directory mostly contains public_html, public_ftp and other folders).
You need to move Laravel scructure into ../ROOT directory as follows:
app/
bootstrap/
vendors/
public_html/
public_ftp/
(...)
Files form /public folder goes to public_html
public_html/index.php
public_hmtl/.htaccess
public_html/packages/
(...)
Then modification for /bootstrap/paths.php is required for line with 'public' key:
'public' => DIR.'/../public' to 'public' => DIR.'/../public_html'
If someone has similar issue and this solution does not work please let me know, thanks.
If you want without public on uri, using composer from your laravel root. Use command php artisan serve and browse localhost:8000 from your browser. It will bring you to homepage.
Where did you get this line from?
RewriteRule ^(.*)$ public/$1 [L]
It's not part of Laravel. Try removing it or the whole .htaccess file in your Laravel root.
I have just deployed a Laravel build to a staging server and am having issues with my URLs to my images folder.
The issue was not present on my local dev environment.
The images on both servers are located in www_root/public/img/ and in the page source are being referenced by /img/xxxx.jpg
As mentioned above, this is all working correctly on local dev but not on my staging environment and the images are not found.
On the staging environment, if I change image links to /public/img/xxxx.jpg they are found but if I do this to local dev it breaks the image refs.
The htaccess at www_root contains:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Rewrite to 'public' folder
RewriteCond %{HTTP_HOST} ^dev.domain.com$ #on the staging environment, this is updated accordingly
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
</IfModule>
and I have updated my application/config/application.php file accordingly.
Aside from the above 2 changes between environments, everything else is exactly the same.
I can't see anywhere else there could be an issue.
Any ideas?
First thing that comes to mind for me, is to use URL::asset('link/to/asset.jpg') in your templates.
Because then you create absolute URLs
When I tried using your provided HTACCESS file, it broke all my links too. Try this one:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public/
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
Hello I am new to laravel and i know laravel has new version but i am using version 3.
Now all i need to do is directly access public folder without typing /pulic in url.
I have placed below .htaccess file in laravel root but it giving me 404 error.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
and i want to do this using .htaccess not using virtualhost.
I think you should make your site's document root point to public from your server config (vhost on apache, not sure what it's called on nginx)