在 CentOS 下,如果你说的是 Apache(httpd) 开启伪静态(Rewrite),常见做法如下(以 CentOS 7/8 + Apache 2.4 为例):
httpd -v
# 或
apachectl -v
如果没装:
yum install httpd -y
# CentOS 8 用
dnf install httpd -y
CentOS 默认 Apache 一般已加载 mod_rewrite,可确认:
httpd -M | grep rewrite
如果有 rewrite_module,说明已开启。
如未开启(很少见):
vi /etc/httpd/conf/httpd.conf
找到并取消注释:
LoadModule rewrite_module modules/mod_rewrite.so
编辑虚拟主机或主配置:
vi /etc/httpd/conf/httpd.conf
或你的站点配置(如 /etc/httpd/conf.d/xxx.conf)
确保有:
<Directory "/var/www/html">
AllowOverride All
</Directory>
⚠️
AllowOverride None会导致伪静态失效
systemctl restart httpd
在网站根目录(如 /var/www/html)创建:
vi /var/www/html/.htaccess
RewriteEngine On
RewriteRule ^article/([0-9]+)\.html$ article.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
AllowOverride All.htaccess 是否在正确目录chmod 644 .htaccess
setsebool -P httpd_can_network_connect 1
CentOS 默认是 httpd,不是
apache2
apache2是 Ubuntu/Debian 的叫法
如果你用的是 某种框架(WordPress / ThinkPHP / Laravel) 或 面板(宝塔 / cPanel),可以告诉我,我可以给你更精确的配置。