nginx上支持.htaccess伪静态的配置实例
本文介绍下,在nginx上配置.htaccess伪静态的方法,有需要的朋友参考下吧。
在apache上.htaccess转向,只要apache编译的时候指明支持rewrite模块即可。
但是换到nginx上方法会有不同,有人说把.htaccess转向规则写到nginx的配置文件里面,官方提供的方法之一,肯定可行的。
不过,此方法有个问题:不方便,下次要更改一个伪静态转向规则的时候还得去nginx的配置文件或者nginx的虚拟网站的配置文件里面去改,相比apache直接在目录下放置.htaccess文件,nginx的这个办法显然很原始。
解决方法:
在nginx的配置文件中include .htacces文件就可以实现相同的功能了。
举个例子,要把www.jbxue.com的.htaccess迁移到nginx上,几个步骤:
第一步:修改.htaccess文件,因为apache的rewrite转向规则跟nginx的转向规则还是有一些不一样的,典型的不一样有nginx的根目录需要写在每行转向的地址前,每行规则必须以分号(;)结束,301或者404等跳转使用不同的格式。
apache上htaccess转换到nginx上:
复制代码代码示例:
RewriteEngine On
RewriteBase /
RewriteRule ^show-([0-9]+)-([0-9]+)\.html$ index.php?action=show&id=$1&page=$2
RewriteRule ^category-([0-9]+)-([0-9]+)\.html$ index.php?action=index&cid=$1&page=$2
RewriteRule ^archives-([0-9]+)-([0-9]+)\.html$ index.php?action=index&setdate=$1&page=$2
RewriteRule ^(archives|search|reg|login|index|links)\.html$ index.php?action=$1
RewriteRule ^(comments|tagslist|trackbacks|index)-([0-9]+)\.html$ index.php?action=$1&page=$2
rewriteCond %{http_host} ^jbxue.com [NC]
rewriteRule ^(.*)$ http://www.jbxue.com/$1 [R=301,L]
ErrorDocument 404 http://www.jbxue.com/
RewriteBase /
RewriteRule ^show-([0-9]+)-([0-9]+)\.html$ index.php?action=show&id=$1&page=$2
RewriteRule ^category-([0-9]+)-([0-9]+)\.html$ index.php?action=index&cid=$1&page=$2
RewriteRule ^archives-([0-9]+)-([0-9]+)\.html$ index.php?action=index&setdate=$1&page=$2
RewriteRule ^(archives|search|reg|login|index|links)\.html$ index.php?action=$1
RewriteRule ^(comments|tagslist|trackbacks|index)-([0-9]+)\.html$ index.php?action=$1&page=$2
rewriteCond %{http_host} ^jbxue.com [NC]
rewriteRule ^(.*)$ http://www.jbxue.com/$1 [R=301,L]
ErrorDocument 404 http://www.jbxue.com/
转换成nginx的规则
复制代码代码示例:
rewrite ^/show-([0-9]+)-([0-9]+)\.html$ /index.php?action=show&id=$1&page=$2;
rewrite ^/category-([0-9]+)-([0-9]+)\.html$ /index.php?action=index&cid=$1&page=$2;
rewrite ^/archives-([0-9]+)-([0-9]+)\.html$ /index.php?action=index&setdate=$1&page=$2;
rewrite ^/(archives|search|reg|login|index|links)\.html$ /index.php?action=$1;
rewrite ^/(comments|tagslist|trackbacks|index)-([0-9]+)\.html$ /index.php?action=$1&page=$2;
if ($host != 'www.jbxue.com' ) {
rewrite ^/(.*)$ http://www.jbxue.com/$1 permanent;
}
error_page 404 http://www.jbxue.com/;
rewrite ^/category-([0-9]+)-([0-9]+)\.html$ /index.php?action=index&cid=$1&page=$2;
rewrite ^/archives-([0-9]+)-([0-9]+)\.html$ /index.php?action=index&setdate=$1&page=$2;
rewrite ^/(archives|search|reg|login|index|links)\.html$ /index.php?action=$1;
rewrite ^/(comments|tagslist|trackbacks|index)-([0-9]+)\.html$ /index.php?action=$1&page=$2;
if ($host != 'www.jbxue.com' ) {
rewrite ^/(.*)$ http://www.jbxue.com/$1 permanent;
}
error_page 404 http://www.jbxue.com/;
第二步:修改nginx的配置文件,增加include该.htaccess文件
vi /etc/nginx/sites-available/www.jbxue.com
增加一行:
复制代码代码示例:
include /var/www/www.jbxuecom/.htaccess
修改为相应的地址。
第三步:测试并重启
复制代码代码示例:
/etc/init.d/nginx -configtest
重启生效:
复制代码代码示例:
/etc/init.d/nginx restart
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构