#
ドキュメント

Document

自分のための備忘録です。

.htaccess

RedirectとRewrite

RedirectとRewrite : WordPress

Rewrite

mod_rewriteが有効な場合にのみ処理をする。

<IfModule mod_rewrite.c>
    RewriteEngine on
    # 処理
</IfModule>

http -> https

blogディレクトリを除いてSSL化。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !on
    RewriteCond %{REQUEST_URI} !(^/blog/)
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

.htaccess上では、リダイレクト元のパターン指定は、ホスト名直後のスラッシュを含まないURLパス、逆に、リダイレクト先はホスト名直後のスラッシュを含むパスとなります。これはApache の仕様で定義されていますが、混乱しやすいため、注意が必要です。

参考:RewriteRule directive: What is matched?

ref. .htaccess でのリダイレクト(転送)設定の書き方

Redirect

特定ページをRedirect

通常

301リダイレクトの記述

Redirect permanent /sample/ http://wwww.example.com/index.php

302リダイレクトの記述

Redirect 302 /sample/ http://wwww.example.com/index.php
正規表現

301リダイレクトの記述

RedirectMatch permanent ^/index\.html$ https://example.com/example/

302リダイレクトの記述

RedirectMatch 302 ^/index\.html$ https://example.com/example/

オリジン間リソース共有 (CORS)

Header set Access-Control-Allow-Origin "*"

Filesディレクティブ

<Files ~ "index.php">
    order deny,allow
    allow from all
</Files>

FilesMatchディレクティブ

<FilesMatch "^composer|^COPYING|^\.env|^\.maintenance|^Procfile|^app\.json|^gulpfile\.js|^package\.json|^package-lock\.json|web\.config|^Dockerfile|\.(ini|lock|dist|git|sh|bak|swp|env|twig|yml|yaml|dockerignore)$">
    order allow,deny
    deny from all
</FilesMatch>

ref.

  • https://www.javadrive.jp/apache/section/index2.html
  • https://piro791.blog.ss-blog.jp/2010-03-31-1

order allow deny

https://qiita.com/Shi-nakaya/items/8a9b5c9302cc410251f6