三二互联专业提供速度最快最稳定的美国服务器、香港服务器。中美直连,亚洲优化![ 代理登陆 ] [ 付款方式 ] [ 找回密码 ][ 电子协议责任书 ]
硬件资源保障

采用高配品牌服务器

主流强悍CPU配置

确保服务高速稳定运行

中美直连线路

中美直连亚洲优化

采用中国CN2骨干网络

保证速度飞快稳定高效

如何在Apache环境中配置伪静态?

美国、香港服务器

如何在Apache环境中配置伪静态?

04-07   来源:

 在 Apache 环境配置伪静态(URL 重写),核心是 启用 mod_rewrite 模块 + 配置 .htaccess 规则。下面分 宝塔面板(最常用) 和 手动命令行 两种方式,一步一步教你。

一、宝塔面板 Apache 伪静态配置(推荐)
1. 确认 mod_rewrite 已开启
软件商店 → Apache → 设置 → 模块管理
确保 rewrite 模块已勾选(宝塔默认一般已开)
2. 网站伪静态入口
网站 → 你的站点 → 设置
左侧 → 伪静态
3. 直接粘贴规则(通用模板)
把下面代码粘贴到伪静态框,保存 → 重启 Apache。
(1)通用伪静态(PHP 框架 / 单入口)
apache
RewriteEngine On
RewriteBase /
 
# 存在文件/目录则直接访问,不重写
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
 
# 所有请求转发到 index.php(ThinkPHP / Laravel / 自定义框架)
RewriteRule ^(.*)$ index.php/$1 [L]
(2)动态转静态示例(article.php?id=123 → /article/123)
apache
RewriteEngine On
RewriteBase /
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/(\d+)$ article.php?id=$1 [L]
(3)HTML 后缀伪静态
apache
RewriteEngine On
RewriteBase /
 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
二、手动 Apache 伪静态(无面板)
1. 启用 mod_rewrite
CentOS/RHEL
bash
运行
# 编辑主配置
vi /etc/httpd/conf/httpd.conf
# 找到并去掉注释
LoadModule rewrite_module modules/mod_rewrite.so
 
# 重启
systemctl restart httpd
Ubuntu/Debian
bash
运行
a2enmod rewrite
systemctl restart apache2
2. 允许 .htaccess(关键)
编辑站点虚拟主机配置:
apache
    AllowOverride All
    Require all granted
再重启 Apache。
3. 网站根目录新建 .htaccess
bash
运行
cd /var/www/html
vi .htaccess
粘贴上面任意一套规则,保存即可。
三、常用伪静态规则(直接复制)
1. WordPress
apache
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
2. ThinkPHP 5/6
apache
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
3. 强制 HTTPS
apache
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
四、验证是否生效
访问伪静态 URL(如 /article/123)
F12 → Network → 看响应头无 404
查看 Apache 日志:
bash
运行
# CentOS
tail -f /var/log/httpd/error_log
# Ubuntu
tail -f /var/log/apache2/error.log
五、常见问题
403 Forbidden:.htaccess 权限不对 → chmod 644 .htaccess
规则不生效:mod_rewrite 未开 / AllowOverride All 未配
死循环 / 500:规则冲突,检查 [L] 与顺序

三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快

上一篇:如何在Apache环境中配置伪静态(URL重写)? 下一篇:如何在Apache环境中配置304协商缓存的缓存时间为永久?

美国GIA服务器三二互联版权所有 WWW.250.cc 2008-2015 All Rights Reserved
三二互联 - 专业的美国C3服务器香港vps、抗DOOS流量清洗、云备份系统、网站加速系统、美国GIA服务器和香港云服务器产品提供商
三二互联24小时在线工单系统为您提供全面、专业、周到的技术支持与服务
咨询热线:400-679-9994(免长话费)