Search code examples
wordpress.htaccess

htaccess - Subdomain to wordpress page - keep original url


I have a wordpress site (example.com) for our sportsclub and want to use different subdomains (football, volleyball) to route to its own page example.com/football;

but the URL should still show football.example.com/

The other pages like /terms, /contact or /location are all the same. So if there is a request for example.com/terms or volleyball.example.com/terms it should all link to the same page but keep the original URL with its original subdomain.

I tried:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /


RewriteCond %{HTTP_HOST} ^football.example.com [NC]
RewriteRule ^(.*) http://example.com/$1 [P, L]

RewriteCond %{HTTP_HOST} ^volleyball.example.com [NC]
RewriteRule ^(.*) http://example.com/$1 [P, L]

</IfModule>


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

my apache2.conf has the following Options for this Directory

  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted

The P Option allone redirects me to the right page when i request a specific page in the URL but the url changes; if i dont request a page and only use volleyball.example.com I get a Not Found Error message from Wordpress.

P and L combined gets me an 500 Internal Server Error with following Text in the log File

RewriteRule: bad flag delimiters

Solution

  • <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} ^/$
    RewriteCond %{HTTP_HOST} (football|volleyball)\.example\.com$ [NC]
    RewriteRule ^(.*) http://%1.example.com/%1 [L]
    
    
    </IfModule>
    
    #BEGIN Wordpress
    [original Wordpress Content]
    #END Wordpress
    
    

    This code is nearly all I want. If I dont request a specific page, and request volleyball.example.com I get redirected to volleyball.example.com/volleyball and if I request volleyball.example.com/terms i will get to the terms.