James
James

Reputation: 16339

Using .htaccess to redirect all requests to subdomain

I'm hosting a Laravel application on my server and have set up a subdomain to host it in my virtual host.

I have another subdomain on my server and after hours of playing around trying to set up an .htaccess file, I came up with the below which redirects all requests to www.mysite.net/example to my subdomain example.mysite.net (e.g www.mysite.net/example/12345 goes to example.mysite.net/12345)

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?(mysite\.net)$ [NC]
RewriteRule ^(.*)$ http://example.%1/$1 [R=301,L,NE]

I'm wanting to tweak this to work with Laravel, but it doesn't quite work the same considering Laravel is hosted out of the following path mysite.net/laravel/public rather than mysite.net/example.

How would I edit the above .htaccess to redirect all requests to mysite.net/laravel/public to laravel.mysite.net? I.e mysite.net/laravel/public/12345 would redirect to laravel.mysite.net/12345

Edit Here is the Virtual Host I have added through Apache

<VirtualHost *:80>
        ServerName laravel.mysite.net
        DocumentRoot /var/www/laravel/public
        <Directory /var/www/laravel/public>
            Options -Indexes
        </Directory>
</VirtualHost>

Upvotes: 1

Views: 505

Answers (2)

Rahul Singh
Rahul Singh

Reputation: 892

You can use redirect rule of htaccess to redirect any directory to a different domain. Use the below code:

Redirect 302 /laravel/public/ http://laravel.mysite.net/

Please let me know if it helps you out.

Upvotes: 0

anubhava
anubhava

Reputation: 785471

Place this rule as your very first rule inside /var/www/laravel/public/.htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?mysite\.net$ [NC]
RewriteCond %{THE_REQUEST} /laravel/public/(\S*)\s [NC]
RewriteRule ^ http://laravel.mysite.net/%1 [L,R=302]

Upvotes: 1

Related Questions