Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subdomain route to subfolder with htaccess on wordpress

I am trying to create a subdomain sub.domain.com that displays domain.com/sub without changing the address bar.

I've come up with the following .htaccess rule based on other questions made here:

RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/$1 [L,NC]

The problem is I'm using a WordPress instance and my .htaccess currently looks like this now:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/$1 [L,NC]

But when I browse sub.domain.com I'm being redirected to my homepage (domain.com) from WordPress. How can I do this correctly?


Tried to change it to:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^((?!sub/).*)$ /sub/$1 [NC]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>

But it is still not working

like image 426
J0ker98 Avatar asked Dec 06 '19 16:12

J0ker98


1 Answers

You have to create a virtual host in apache config.

<VirtualHost *:80>
    ServerName sub.domain.com
    DocumentRoot /var/www/sub
</VirtualHost>

Or VirtualDocumentRoot if mod_vhosts_alias is enabled.

<VirtualHost *:80>
    ServerName sub.domain.com
    ServerAlias *.domain.com
    VirtualDocumentRoot /var/www/%1
</VirtualHost>

In above I am pointing it to /sub directory if it exists. After all changes restart the server.

like image 110
Abhishek Gurjar Avatar answered Oct 04 '22 20:10

Abhishek Gurjar