简体   繁体   中英

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 (eg 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 ? Ie 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>

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]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM