简体   繁体   中英

Apache Mod_Rewrite

I'm struggling writing a .htaccess file to basically map the following domains:

  • dev.domain.com/$1 => index.php/dev/$1
  • api.domain.com/$1 => index.php/api/$1
  • domain.com/$1 => index.php/front/$1
  • www.domain.com/$1 => index.php/front/$1

At the moment, everything is mapping to index.php/$1 with the following configuration:

<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

This is what came with the php framework I'm using by default. Any help would be greatly appreciated :-) Thanks!

If you want to map the requested paths based on the domain name, then you can use an additional RewriteCond and match the HTTP_HOST first.

You will have to multiply your block of existing RewriteConds and the RewriteRule for each domain - except for the last pair which could just be a default:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^dev\.domain\.com
RewriteRule ^(.*)$ index.php/dev/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^api\.domain\.com
RewriteRule ^(.*)$ index.php/api/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/front/$1 [L]

Not tested. But see also the article on serverfault Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask? , which explains the interactions between conditions and rewrite rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM