Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

4wk_'s avatar
Level 6

RewriteRule /public/* in Laravel raising concerns..

Hello,

For years, foobar.com was a handmade PHP website, stored on server A, in /var/www/foobar. Tomorrow, foobar.com will be a Laravel website, stored on server B, in /srv/foobar/. Of course, Apache vhost is set up to go to /srv/foobar/public/ folder.

For years, we sent to users URLs like foobar.com/public/xxx in emails, pdfs, etc (= we can't change any of that), to allow users to download resources (files, pdf, etc). => We want those URLs to keep working when we deploy the new website.

Setting up a new domain foobarold.com, to server A and adding a rewrite rule in Laravel /srv/foobar/public/.htaccess is working well:

RewriteRule ^public/(.*)$ https://foobarold.com/public/ [QSA,R,L]

=> all request foobar.com/public/xxx URLs are redirected to foobarold.com/public/xxx and users are able to download their files.

My BIG concern is messing up something with Laravel's internal stuff.

a. As explained, all HTTP requests handled by Apache are going into the Laravel public/ folder, so an URL like foobar.com/public/* should not make any sense (except if /srv/foobar/public/public exists, of course, which is not the case). b. Any Laravel-related file actually stored in /srv/foobar/public/ is, AFAIK, always called without "public" (cf a.). For instance, the file /srv/foobar/public/js/main.js is called src="/js/main.js" in views.

But still... I'm afraid I miss something here and that this RewriteRule will mess something up... I've searched on Laracast and on Stackexchange network, but I didn't find anything close to this.

What do you guys think? Thank you in advance for sharing your opinion!

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Your concern is valid, but the RewriteRule you've set up should not interfere with Laravel's internal routing. Laravel's routing is handled by the application itself, not by the .htaccess file in the public directory.

The .htaccess file is used by Apache to handle incoming requests before they reach Laravel. In your case, it's redirecting requests for foobar.com/public/* to foobarold.com/public/*, which is exactly what you want.

As long as you don't have a route in your Laravel application that starts with public/, there should be no conflict. Laravel's routes are relative to the public directory, so a route like Route::get('/public/something', ...) would correspond to a URL like foobar.com/public/something.

If you're still concerned, you could add a condition to your RewriteRule to exclude any requests for actual files or directories in the public directory. Here's how you could do that:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^public/(.*)$ https://foobarold.com/public/ [QSA,R,L]

This will only redirect requests that don't correspond to an existing file or directory. This way, if you ever do add a file or directory in your public directory that starts with public/, it won't be redirected.

1 like

Please or to participate in this conversation.