nginx.ccWeb server troubleshooting desk
Home / Generator / Rewrites / Discuz! X

Discuz! X nginx rewrite rules

nginx rewrite rules for Discuz! X, generated for your domain, root, PHP-FPM and HTTPS as a full server block or snippet, with .htaccess versions and cited sources.

Community-standard rules Source: Discuz! admin “Global → SEO → Static URLs → view current rewrite rules”

Nginx · Discuz! X

Save as /etc/nginx/conf.d/example.com.conf · Apply: nginx -t && systemctl reload nginx
server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    location ^~ /.well-known/acme-challenge/ {
        root /var/www/_letsencrypt;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    # Mozilla Intermediate (Mozilla Server Side TLS 6.0)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
    ssl_prefer_server_ciphers off;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;
    # No OCSP stapling: Let's Encrypt shut down its OCSP service in 2025.

    root /var/www/example.com;
    index index.php index.html;
    server_tokens off;
    client_max_body_size 50m;

    # Security headers (server level only; a location with its own add_header must repeat the whole set)
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
    # CSP must be tailored per site; enable it once you are sure it does not block your own scripts:
    # add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always;
    add_header Strict-Transport-Security "max-age=63072000" always;

    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 256;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml;

    # Same as the nginx rules Discuz! X3.x generates in its admin ([^\.]* also covers subdirectories)
    rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
    rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
    rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
    rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
    rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
    rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
    rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;

    # No PHP execution in upload/cache directories
    location ~* ^/(data|uc_server/data|static)/.*\.php$ {
        deny all;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
        fastcgi_hide_header X-Powered-By;
    }

    # Hidden files (.git, .env, …)
    location ~ /\.(?!well-known) {
        deny all;
    }
}

Notes

  • Your own admin page (“view current Rewrite rules”) is authoritative: keep only the rules for the URL types you enabled.
  • No Caddy equivalent is provided for Discuz!'s rule set.

Apache .htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)topic-(.+)\.html$ $1portal.php?mod=topic&topic=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)article-([0-9]+)-([0-9]+)\.html$ $1portal.php?mod=view&aid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)forum-(\w+)-([0-9]+)\.html$ $1forum.php?mod=forumdisplay&fid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1forum.php?mod=viewthread&tid=$2&extra=page\%3D$4&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)group-([0-9]+)-([0-9]+)\.html$ $1forum.php?mod=group&fid=$2&page=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)space-(username|uid)-(.+)\.html$ $1home.php?mod=space&$2=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)blog-([0-9]+)-([0-9]+)\.html$ $1home.php?mod=space&uid=$2&do=blog&id=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)(fid|tid)-([0-9]+)\.html$ $1archiver/index.php?action=$2&value=$3&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1plugin.php?id=$2:$3&%1

Caddy equivalent

No concise Caddy equivalent; use nginx.

Every generated config passes the Linter; still run nginx -t on the server. Inner pages 404? See 404 & try_files.

FAQ

Where do the Discuz! X nginx rewrite rules go?

Inside the site's server {} block (usually /etc/nginx/conf.d/<domain>.conf, or the panel's rewrite box). Run nginx -t, then systemctl reload nginx.

Discuz! X inner pages still 404 after adding rules?

Check: 1) the rules are in the right server block (server_name matches); 2) reload succeeded; 3) root points at the entry directory; 4) pretty URLs are enabled in the app; 5) no control-panel rewrite file overrides it.

What about Apache for Discuz! X?

This page also shows the .htaccess version; Apache needs mod_rewrite and AllowOverride All.

Anything else to watch for with Discuz! X?

Your own admin page (“view current Rewrite rules”) is authoritative: keep only the rules for the URL types you enabled.

Sponsored slot (empty)
VPS / CDN recommendations may appear here later, labelled as affiliate links. There are no referral links today.