在 CentOS + Apache 下开启“伪静态”(URL Rewrite),一般步骤如下(以常见 PHP 程序如 WordPress / ThinkPHP / Discuz 为例):
httpd -v
systemctl status httpd
如未启动:
systemctl start httpd
systemctl enable httpd
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会导致伪静态失效
修改后重启 Apache:
systemctl restart httpd
在网站根目录创建或编辑:
vi /var/www/html/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
如果开启 SELinux,可能仍无法生效:
getsebool -a | grep httpd
setsebool -P httpd_can_network_connect on
如 .htaccess 不生效也可尝试:
restorecon -Rv /var/www/html
访问:
http://你的IP/文章名/
不再出现 index.php
如果你用的是 具体程序(WordPress / TP / Laravel / Discuz) 或 CentOS 7/8/Stream,可以告诉我,我给你精确配置。