Skip to content

Instantly share code, notes, and snippets.

@hrkd
Last active December 25, 2015 20:29
Show Gist options
  • Save hrkd/7035108 to your computer and use it in GitHub Desktop.
Save hrkd/7035108 to your computer and use it in GitHub Desktop.
htaccess関連。小手先ハック感イヤだけど。
#PHPをhtml拡張子で動作・かつSSI有効
AddType application/x-httpd-php .php .html
AddHandler application/x-httpd-php .php .html
AddOutputFilter INCLUDES .html
RewriteEngine On
RewriteBase /
#RewriteCondでURLの条件指定
#%{REQUEST_URI}でファイル名部分を指定
RewriteCond %{REQUEST_URI} detail.html
#%{QUERY_STRING}でクエリパラメータ部分を指定
RewriteCond %{QUERY_STRING} ^redirect=(.*)$
#上記に一致する場合だけルールを適用。$1などで通常どおり正規表現の()部分を再利用可能。
#%1などでRewriteCondの正規表現で書いた()部分を使用できる。
#転送後のURL末尾に?を付けないと、クエリパラメータが引き継がれる
#[R]フラグでリダイレクト
RewriteRule ^(.*)$ detail.%1.html? [R]
RewriteCond %{REQUEST_URI} detail.(.*).html
#[L]フラグで終了。[R]フラグがない場合はURLの見た目上の書き換えは行わない
RewriteRule detail.(.*).html detail.html?id=$1 [L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment