目次

mod_rewrite

あるドメインにはRewriteを適用しない

以下の設定で、not-rewrite-domain.com へのアクセスには書き換えが適用されない。

<IfModule rewrite_module>
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^not-rewrite-domain\.com
    RewriteRule .* - [L]
    
    # Rewrite Rules
    RewriteRule ^(.*)$ ...
    ...
</IfModule>

もちろん以下のように書いてもいいが、Ruleが複数になってくると逐一書かなければならないので、上記の方が読みやすい。

<IfModule rewrite_module>
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^not-rewrite-domain\.com
    RewriteRule ^(.*)$ ...
    
    RewriteCond %{HTTP_HOST} !^not-rewrite-domain\.com
    RewriteRule ^(.*)$ ...    
    ...
</IfModule>

全てのアクセスを特定のファイルに飛ばす

やりたいこと

.htaccessで、以下のようにすればいい。(index.phpと同じ階層に作成)

<IfModule rewrite_module>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

GETパラメータの「_url」に、URLルート以降の文字列が入っている。
もしhttps://aaa/bbb/にアクセスするなど、URLルート以降の文字列が無かった場合は、GETパラメータに「_url」自体が存在しない。