How to remove /public/ from a Laravel URL

Cover Image for How to remove /public/ from a Laravel URL
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Remove /public/ from a Laravel URL

If you're using Laravel 5 and find the "/public/" fragment in the URL to be awkward when switching between projects, you're not alone. Luckily, there are some easy solutions to remove it without running a VM or setting your document root to the "public" folder.

The .htaccess Method

One common approach is to use the .htaccess file and mod_rewrite module to redirect requests. However, some users have reported encountering a "NotFoundHttpException" error when using this method. It's essential to ensure that mod_rewrite is enabled on your server. To try this method, follow these steps:

  1. Locate the .htaccess file in your Laravel project's root directory.

  2. Open the .htaccess file using a text editor.

  3. Replace the existing content with the following code:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
  1. Save the changes to the .htaccess file.

After implementing this method, you may need to restart your server for the changes to take effect. If you encounter the "NotFoundHttpException" error, you'll need to explore alternative solutions.

Alternative Solutions

If the .htaccess method didn't work for you, don't worry! There are other methods you can try:

Method 1: Using Laravel Valet

If you're using Laravel Valet, a development environment for Mac, removing the "/public/" from your Laravel URL is straightforward. Valet automatically serves your Laravel projects from the "public" directory, so you don't need to modify any server configurations. Simply access your project using the following URL format: http://projectname.test.

Method 2: Server Configuration

Another solution involves adjusting your server configuration. This method may vary depending on the web server software you're using. Here's an example for Apache:

  1. Open your Apache configuration file (e.g., httpd.conf or apache2.conf).

  2. Locate the "DocumentRoot" directive and change it to point directly to your Laravel project's "public" folder.

  3. Save the configuration file and restart your Apache server.

Once the server is restarted, your Laravel project should be accessible without the "/public/" segment in the URL.

Conclusion

Removing the "/public/" segment from a Laravel URL can greatly improve the readability and convenience of your project. While the .htaccess method is the most common approach, it may not always work as expected. In such cases, you can try alternative solutions like Laravel Valet or adjusting your server configuration.

Experiment with these solutions, and choose the one that best fits your development environment and workflow. Remember to back up your project before making any changes to avoid any unintended consequences.

Now that you have the tools to remove the "/public/" from your Laravel URLs, go ahead and customize your URLs like a pro! Let us know in the comments which method worked best for you or if you have any other tips to share.

Happy coding! 💻🚀


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello