.htaccess rewrite subdomain to directory and keep subdomain in url

前端 未结 1 781
耶瑟儿~
耶瑟儿~ 2020-12-30 12:15

I need to rewrite a subdomain to a subdirectory using .htaccess but keep the subdomain in the url like this:

Visited url in browser: sub1.domain.com

相关标签:
1条回答
  • 2020-12-30 13:13

    You can use this rule in document root:

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

    Explanation:

    • NC: Ignore case
    • L: Last rule
    • RewriteCond %{HTTP_HOST} line makes sure that rule only executes for subdomain
    • (?!sub1/) is negative lookahead expression that means of request is not starting with /sub1/
    • RewriteRule ^((?!sub1/).*)$ /sub1/$1 [L,NC] rewrites current path to /sub1/<uri>

    References:

    • Apache mod_rewrite Introduction
    • Apache mod_rewrite Technical Details
    0 讨论(0)
提交回复
热议问题