.htaccess Wildcard Subdomains

后端 未结 3 1090
终归单人心
终归单人心 2020-12-09 14:04

I\'m trying to implement a solution using .htaccess and wildcard subdomains so that

http://subdomain.example.com is mapped to http://example.com/index.php/accounts/s

相关标签:
3条回答
  • 2020-12-09 14:44

    You probably need to exclude the index.php from your rule:

    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
    RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]
    
    0 讨论(0)
  • 2020-12-09 14:54

    Try changing your RewriteRule to

    RewriteRule ^/(.*)$ /index.php/accounts/%1/$1 [PT]
    

    That will rewrite the URL to one that includes the subdomain and the original request URI.

    EDIT: maybe it needs to be

    RewriteRule ^(.*)$ /index.php/accounts/%1/$1 [PT]
    

    as mentioned in the comments.

    0 讨论(0)
  • 2020-12-09 14:56

    This is an adaptation of the code I use to redirect subdomains on my own site. I make no claims to it being best practice but it works;

    RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
    RewriteCond %1 !^(www)\.example$ [NC]
    RewriteRule ^.*$ http://www.example.com/index.php/accounts/%1/ [R=301,L]
    
    0 讨论(0)
提交回复
热议问题