skip to Main Content

I had a laravel project on a sub-domain and I migrated to another sub-domain.

I want to redirect all requests from old sub-domain to the new one.

For example:

https://old.website.com
https://old.website.com/login
https://old.website.com/contact

Should become:

https://new.website.com
https://new.website.com/login
https://new.website.com/contact

I moved all the files to another folder and added .htaccess with the following code:

RewriteEngine On
RewriteRule ^(.*)$ https://new.website.com/$1 [R=301,L]

But the issue is that public is added to the url for example:

https://old.website.com

Becomes:
https://new.website.com/public

So I get 404 not found error.

For the sub-domain I set the document root to point to public folder so no need to add the public directory to the url.

2

Answers


  1. By the sounds of it, you likely have an internal rewrite to the /public subdirectory. Try the following instead:

    RewriteRule ^ https://new.website.com%{REQUEST_URI} [R=301,L]
    

    Test with 302 (temp) redirect first to avoid potential caching issues. You will need to clear your browser cache before testing.

    Login or Signup to reply.
  2. I have this solution on one of my projects. To redirect requests from old sub-domain to the new one without the public folder in the URL, you could need to modify the .htaccess file. You can try the following code:

        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^old.website.com$ [OR]
        RewriteCond %{HTTP_HOST} ^www.old.website.com$
        RewriteRule (.*)$ https://new.website.com/$1 [R=301,L]
    

    This should redirect all requests to the new sub-domain while preserving the rest of the URL. This way, you should be able to access the pages at their new URL without encountering a 404 error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search