URL重写规则调整需求:添加域名格式主题名称参数
Hey there! Let's work through your rewrite rule issue and get it sorted properly.
First, let's address the immediate problem with your initial proposed rule:
RewriteRule ^topic/([^/]+)$ index.php?page=topic&server_id=$1&name=$2
This won't work because you only have one capture group (([^/]+)), so $2 will be empty. You need to adjust the regex to capture two separate values (the topic name and ID) from the URL path.
Good news about domain-formatted names
Your concern about matching names like example.com is totally valid, but the [^/]+ pattern works perfectly here! That regex matches any character except a forward slash, which includes dots (.), hyphens, and all other characters in a domain name. No issues there.
The Correct RewriteRule Setup
Since you need to pass two parameters in the order topic name first, ID second, your URL structure should look like this:/topic/your-domain-name.com/123
To capture both values, use a regex with two capture groups separated by a slash:
RewriteRule ^topic/([^/]+)/([^/]+)$ index.php?page=topic&name=$1&server_id=$2
([^/]+)captures the topic name (e.g.,your-domain-name.com) into$1- The second
([^/]+)captures the ID (e.g.,123) into$2 - We map these to the
nameandserver_idparameters as needed
Optional: Restrict ID to Numbers (If Applicable)
If your server_id is always a numeric value, you can make the regex more precise to avoid unintended matches:
RewriteRule ^topic/([^/]+)/(\d+)$ index.php?page=topic&name=$1&server_id=$2
(\d+) only matches one or more digits, which adds a layer of validation.
Important Notes
- Make sure your Apache config has
mod_rewriteenabled and that you haveRewriteEngine Onat the top of your.htaccessor config file. - If you absolutely can't use a slash to separate the two parameters (not recommended, since domains contain dots), you'd need a different separator that doesn't appear in either the name or ID—but using slashes is the cleanest, most standard approach for URL path segments.
内容的提问来源于stack exchange,提问作者Marcelino Pietero

