在 CentOS + Apache 下配置“伪静态”(URL Rewrite),通常是通过 mod_rewrite 模块实现的。下面按最常见场景给你一个完整流程。
httpd -v
# 或
apachectl -v
httpd -M | grep rewrite
如果看到:
rewrite_module (shared)
说明已启用。
如果没有,编辑配置文件:
vi /etc/httpd/conf/httpd.conf
取消注释(删掉 #):
LoadModule rewrite_module modules/mod_rewrite.so
找到你的站点配置(如 /etc/httpd/conf/httpd.conf 或 /etc/httpd/conf.d/*.conf)
示例(重要):
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
⚠️ AllowOverride None 会导致 .htaccess 失效
vi /var/www/html/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
✅ 效果:
/article/123
→
/index.php/article/123
RewriteRule ^news/([0-9]+)\.html$ news.php?id=$1 [L]
访问:
/news/123.html
实际:
/news.php?id=123
RewriteRule ^cat/([a-z]+)$ cat.php?name=$1 [L]
systemctl restart httpd
或
service httpd restart
AllowOverride All.htaccess 在网站根目录tail -f /var/log/httpd/error_log
通常是 SELinux 或权限问题:
setenforce 0 # 临时关闭(测试用)
或:
chmod -R 755 /var/www/html
URL_MODEL = 2如果你能告诉我:
我可以给你精准配置。