banner
阿珏酱

阿珏酱

乘上与平常相反的电车,去看看那未曾见过的风景
twitter
github
facebook
bilibili
zhihu
steam_profiles
youtube

.htaccess伪静态规则

Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解`

.htaccess 伪静态规则

日期:2017-12-4 阿珏 折腾代码 浏览:1701 次 评论:1 条

image Apache的 mod_rewrite是比较强大的,在进行网站建设时,可以通过这个模块来实现伪静态。

检测 Apache 是否开启 mod_rewrite 功能#

可以通过 php 提供的 phpinfo () 函数查看环境配置,找到 “Loaded Modules”,其中列出了所有 apache2handler 已经开启的模块,如果里面包括 “mod_rewrite”,则已经支持,不再需要继续设置。

如果没有开启 “mod_rewrite”,则打开目录 apache 目录下的 “/apache/conf/” , 找到 httpd.conf 文件,再找到 “LoadModule rewrite_module”,将前面的”#” 号删除即表示取用该功能。

如果没有查找到 “LoadModule” 区域,可以在最后一行加入 “LoadModule rewrite_module ,modules/mod_rewrite.so”(独占一行),之后重启 apache 服务器。
再通过 phpinfo () 函数查看环境配置就有 “mod_rewrite” 为项了.。

让 apache 服务器支持.htaccess#

如何让自己的本地 APACHE 服务器支持:“htaccess” 呢?只需修改 apache 的 httpd.conf 设置就可以让 APACHE 支持 “.htaccess” 了。打开 APACHE 目录的 CONF 目录下的 httpd.conf 文件,找到: Options FollowSymLinks AllowOverride None 改为 Options FollowSymLinks AllowOverride All 就行了。

建立.htaccess 文件#

建立.htaccess 文件时要注意,不能直接建,方法是通过记事本中的另存为菜单,在文件名窗口输入:“.htaccess”,然后点击保存。

rewrite 规则学习#

RewriteEngine on #rewriteengine 为重写引擎开关 on 为开启 off 为关闭

RewriteRule ([0-9]{1,})$index.php?id=$1 在这里,RewriteRule 是重写规则,是用正则表达式的句子,([0-9]{1,}) 表示由数字组成的,$ 表示结束标志,表示以数字结束!

如果要实现伪静态页面,规则如下:

RewriteEngine on RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$index.php?action=$1&id=$2

以下是我博客的 .htaccess#

<IfModule mod_rewrite.c>
RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^post-([0-9]{1,}).html$ index.php?post=$1 
</IfModule>

http 强制转跳 https 的规则#

RewriteEngine on
RewriteBase / 
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

网友评论:

image 广东自考网 6 个月前 (2020-08-28)
学习到了 感谢博主的分享

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。