.htaccess rewrite: subdomain as GET var and path as GET var

后端 未结 1 1386
庸人自扰
庸人自扰 2020-12-05 21:31

Desired result:

http://example.com/                 -> index.php
http://www.example.com/             -> index.php
http://hello.example.com/           -         


        
相关标签:
1条回答
  • 2020-12-05 21:59

    Use the first rule to add the subdomain parameter, without changing the URI, then use the 2nd rule to route the URI to index.php:

    RewriteEngine On
    
    # Parse the subdomain as a variable we can access in PHP, and
    # run the main index.php script
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{HTTP_HOST}        !^www
    RewriteCond %{HTTP_HOST}         ^([^\.]+)\.([^\.]+)\.([^\.]+)$
    RewriteRule ^(.*)$ /$1?subdomain=%1
    
    # Map all requests to the 'path' get variable in index.php
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA] 
    

    The second rule needs to have the QSA flag, otherwise the first rule's query string gets lost.

    0 讨论(0)
提交回复
热议问题