htaccess文件的作用(htaccess伪静态文件)

htaccess文件的作用(htaccess伪静态文件)

扫码添加渲大师小管家,免费领取渲染插件、素材、模型、教程合集大礼包!

.htaccess文件是运行Apache Web Server的Web服务器的配置文件。对配置和重定向Apache Web Server文件系统很有用。

请记住.htaccess文件将采用隐藏格式。没有人可以通过URL直接看到它。

.htaccess文件有很多用途。在这里。我将讨论创建它们及其用途。

htaccess文件的作用(htaccess伪静态文件)

创建.htaccess文件

打开任何文本编辑器并使用名称保存文件.htaccess。mod_rewrite在Apache Web服务器配置中的php.ini文件中启用扩展。

用于.htaccess

1.禁用目录列表

如果要禁用文件夹文件列表。请包括以下代码。

#禁用目录列表

Options All –Indexes

2.错误页面

您可以为特定错误设置错误页面。

示例:

errorDocument 400 http://www.yourdomain.com/error.html

errorDocument 401 http://www.yourdomain.com/error.html

errorDocument 404 http://www.yourdomain.com/error.html

errorDocument 500 http://www.yourdomain.com/error.html

如果要在Apache服务器中启用“重写规则”。则必须编写:RewriteEngine on

如果要关闭此规则。请将值更改为关闭。

RewriteEngine on

3.域重定向

要将yourdomain.com永久重定向到www.yourdomain.com。请添加以下代码:

RewriteCond%{HTTP_HOST} ^ yourdomain.com

RewriteRule(。*)http://www.yourdomain.com / $ 1 [R = 301。L]

4.子域重定向

您还可以执行子文件重定向映射到该文件夹。这里www.yourdomain.com正在连接到该 website_folder 文件夹。

RewriteCond%{HTTP_HOST} ^ www \ .yourdomain \ .com $

RewriteCond%{REQUEST_URI}!^ / website_folder /

RewriteRule(。*)/ website_folder / $ 1

这里subdomain.yourdomain.com正在连接到该 subdomain_folder 文件夹。

RewriteCond%{HTTP_HOST} ^ subdomain \ .yourdomain \ .com $

RewriteCond%{REQUEST_URI}!^ / subdomain_folder /

RewriteRule(。*)/ subdomain_folder / $ 1

5.旧域重定向

用于将旧域(old.com)重定向到新域(new.com)的.htaccess代码。

RewriteCond%{HTTP_HOST} ^ old.com

RewriteRule(。*)http://www.new.com/$1 [R = 301。L]

RewriteCond%{HTTP_HOST} ^ www \ .old \ .com

RewriteRule(。*)http://www.new.com/$1 [R = 301。L]

6.友好的URL

友好和信息丰富的URL有助于搜索引擎排名。URL是搜索引擎优化过程中非常重要的一部分。

个人资料网址
配置文件参数[ a-zA-Z0-9_- ]会打开此输入(有关更多帮助。请阅读正则表达式):
http://gurutechnolabs.com/profile.php?username=test成http:// gurutechnolabs.com/test

RewriteRule ^([a-zA-Z0-9 _-] +)$ profile.php?username = $ 1

RewriteRule ^([a-zA-Z0-9 _-] +)/ $ profile.php?username = $ 1

朋友网址
http://gurutechnolabs.com /friends.php?username=test到
http://gurutechnolabs.com/friends/test

RewriteRule ^ friends /([a-zA-Z0-9 _-] +)$ friends.php?username = $ 1

RewriteRule ^ friends /([a-zA-Z0-9 _-] +)/ $ friends.php?username = $ 1

具有两个参数的朋友URL
这里第一个参数允许[a-zA-Z0-9_-]。第二个参数只允许数字[ 0-9 ]

http://gurutechnolabs.com/friends.php?username=test&page=2至http://gurutechnolabs.com/friends/test/2

RewriteRule ^ friends /([a-zA-Z0-9 _-] +)/([0-9] +)$ friends.php?username = $ 1&page = $ 2

RewriteRule ^ friends /([a-zA-Z0-9 _-] +)/([0-9] +)/ $ friends.php?username = $ 1&page = $ 2

7.隐藏文件扩展名

假设您不想显示网页扩展名。

示例:

http://www.yourdomain.com/index.html至 http://www.yourdomain.com/index只需添加以下代码:

RewriteRule ^([^ /。] +)/?$ $ 1.html

使用.htaccess提高网站速度和性能
1.启用压缩:

压缩对于减小网页大小非常有用。

有两种压缩选项:

Deflate:非常容易设置。
GZIP:功能更强大。您可以预先压缩内容。
要启用Deflate模式。请在.htaccess中添加以下代码

<ifmodule mod_deflate.c>

AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript

</ifmodule>

启用GZIP压缩模式

<ifModule mod_gzip.c>

mod_gzip_on Yes

mod_gzip_dechunk Yes

mod_gzip_item_include file .(html?|txt|css|js|php|pl)$

mod_gzip_item_include handler ^cgi-script$

mod_gzip_item_include mime ^text/.*

mod_gzip_item_include mime ^application/x-javascript.*

mod_gzip_item_exclude mime ^image/.*

mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

</ifModule>

2.添加过期标题:启用浏览器缓存

Expire标头用于缓存来自浏览器的数据。加速网页是有帮助的。因为网页可以从浏览器中检索数据。因此无需从减少HTTP请求的服务器获取数据。

<IfModule mod_expires.c>

ExpiresActive on

#如果要设置默认过期日期

ExpiresDefault“访问加1个月”

#对于html文档

ExpiresByType text / html“access plus 0 seconds”

#数据

ExpiresByType text / xml“access plus 0 seconds”

ExpiresByType application / xml“access plus 0 seconds”

ExpiresByType应用程序/ json“访问加0秒”

# RSS订阅

ExpiresByType应用程序/ rss + xml“访问加1小时”

#Favicon(无法重命名)

ExpiresByType image / x-icon“访问加1周”

#Media:图像。视频。音频

ExpiresByType image / gif“access plus 1 month”

ExpiresByType image / png“access plus 1 month”

ExpiresByType image / jpg“access plus 1 month”

ExpiresByType image / jpeg“access plus 1 month”

ExpiresByType视频/ ogg“访问加1个月”

ExpiresByType音频/ ogg“访问加1个月”

ExpiresByType视频/ mp4“访问加1个月”

#CSS和JavaScript

ExpiresByType text / css“access plus 1 year”

ExpiresByType应用程序/ javascript“访问加1年”

ExpiresByType text / javascript“access plus 1 year”

<IfModule mod_headers.c>

Header append Cache-Control "public"

</IfModule>

</IfModule>

3.启用Keep-Alive以减少HTTP请求

通过启用Keep-Alive。您的Web服务器告诉Web浏览器不需要为它在站点上检索的每个文件单独请求。此外。请确保以减少HTTP请求的方式对其进行编码。

<ifModule mod_headers.c> Header Set Connection Keep-alive <ifModule>

4.防止垃圾邮件机器人抓取您的网站

有时。您的网站的页面加载速度可能会因主机方案中可用的带宽而降低。有时垃圾邮件船会占用您的大部分带宽。而您的网站也会变慢。

因此。防止垃圾邮件是有帮助的。

<ifModule mod_setenvif.c>

#将垃圾邮件发送者引荐为spambot

SetEnvifNoCase Referer spambot1.com spambot=yes

SetEnvifNoCase Referer spambot1.com spambot=yes

#添加任意数量的内容

Order allow,deny

Allow from all

Deny from env=spambot

<ifModule>

因此。我们可以将.htaccess文件用于多种用途。

分享到 :
相关推荐

小视频服务器托管如何选择托管商

当下随着抖音和快手的快速传播。让资讯快速传遍人们的耳朵。同时企业也因此获得了更高的流...

怎么忽略FTB登录来升级WordPress

WordPress相关教程之前也介绍不少了。想必有些站长关注这方面的一直也都了解过。...

如何选择海外服务器(如何选择海外服务器地址)

选择海外服务器的方法:1。明确自身业务的需求。比较并评估各种功能。选择合适的海外服务...

服务器需要网卡吗(服务器需要网卡吗)

大伙儿平常应用电脑上上外网的情况下都会遇到有关网卡的难题。包含有线网卡和无线网卡。那...

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注