# -----------------------------
# Универсальный .htaccess для нескольких PHP-блогов
# + Защита папки /myadminka по IP
# -----------------------------

Options -Indexes
ServerSignature Off
RewriteEngine On
RewriteBase /

# -----------------------------
# 1. HTTPS и WWW редирект
# -----------------------------
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L,NE]

# -----------------------------
# 2. ЧПУ для блога
# -----------------------------
RewriteRule ^post/([^/]+)$ index.php?route=post&slug=$1 [QSA,L]
RewriteRule ^category/([^/]+)$ index.php?route=category&name=$1 [QSA,L]
RewriteRule ^page/(\d+)$ index.php?route=page&num=$1 [QSA,L]
RewriteRule ^contact$ index.php?route=contact [QSA,L]
RewriteRule ^about$ index.php?route=about [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# -----------------------------
# 3. Защита mail.php
# -----------------------------
<Files "mail.php">
    <If "%{REQUEST_METHOD} != 'POST'">
        Require all denied
    </If>
</Files>

RewriteCond %{REQUEST_URI} /mail\.php [NC]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteRule ^ - [R=405,L]

# -----------------------------
# 🔐 4. Доступ к /myadminka — только по IP
# -----------------------------
<Directory "myadminka">
    # Запрещаем всем
    Require all denied

    # Разрешаем только эти IP
    Require ip 77.37.134.141
    Require ip 91.199.160.238
</Directory>

# АЛЬТЕРНАТИВА, если <Directory> не работает (например, в shared-хостинге)
# Используем mod_rewrite:
RewriteRule ^myadminka/(.*)$ - [E=ADMINKA:1]

RewriteCond %{REQUEST_URI} ^/myadminka [NC]
RewriteCond %{REMOTE_ADDR} !^77\.37\.134\.141$
RewriteCond %{REMOTE_ADDR} !^91\.199\.160\.238$
RewriteRule ^ - [R=403,L]

# -----------------------------
# 5. Безопасность: запрет .htaccess, .env и т.п.
# -----------------------------
<Files ~ "^\.">
    Require all denied
</Files>

# Запрет PHP в uploads/images
<Directory "uploads">
    php_flag engine off
</Directory>
<Directory "images">
    php_flag engine off
</Directory>

# -----------------------------
# 6. Заголовки безопасности
# -----------------------------
<IfModule mod_headers.c>
    Header always set X-Content-Type-Options nosniff
    Header always set X-Frame-Options DENY
    Header always set X-XSS-Protection "1; mode=block"
</IfModule>

# -----------------------------
# 7. Кэширование и сжатие (опционально)
# -----------------------------
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresDefault "access plus 2 days"
</IfModule>

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript
</IfModule>
