需要帮助清理.htaccess



我正在帮助一个项目中查看一个.htaccess文件,其中的一些代码似乎是多余的。它能清理一下吗?这是代码:

<Files .htaccess>
order allow,deny
deny from all
</Files>
# Add Caching
<FilesMatch ".(png|gif|js|css)$">
ExpiresActive on
ExpiresDefault "access plus 1 month"
</FilesMatch>
# EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresByType application/x-httpd-php "access plus 1 month"
ExpiresDefault "access 1 month"
</IfModule>
# EXPIRES CACHING ##

# disable directory autoindexing
Options -Indexes
ErrorDocument 400 https://domain.name
ErrorDocument 401 https://domain.name
ErrorDocument 402 https://domain.name
ErrorDocument 403 https://domain.name
ErrorDocument 405 https://domain.name
ErrorDocument 404 /incl/pages/error404.php
ErrorDocument 500 https://domain.name
RewriteEngine On
# https
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [L,R=301]
# Redirect to domain without www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript application/x-httpd-php
</ifmodule>
# END GZIP
# Stop hotlinking.
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} ^https?://([^/]+)/ [NC]
RewriteCond %1#%{HTTP_HOST} !^(.+)#1$
RewriteRule .(jpg|jpeg|png|gif|swf|svg)$ - [NC,F,L]
# Compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

您有两个额外的重写规则,可以合并到一个重定向中以删除https和www。

这是建议的完整版。htaccess:

<Files .htaccess>
order allow,deny
deny from all
</Files>
# Add Caching
<FilesMatch ".(png|gif|js|css)$">
ExpiresActive on
ExpiresDefault "access plus 1 month"
</FilesMatch>
# EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresByType application/x-httpd-php "access plus 1 month"
ExpiresDefault "access 1 month"
</IfModule>
# EXPIRES CACHING ##
# disable directory autoindexing
Options -Indexes
ErrorDocument 400 https://domain.name
ErrorDocument 401 https://domain.name
ErrorDocument 402 https://domain.name
ErrorDocument 403 https://domain.name
ErrorDocument 405 https://domain.name
ErrorDocument 404 /incl/pages/error404.php
ErrorDocument 500 https://domain.name
RewriteEngine On
# remove www and turn on https in the same rule
RewriteCond %{HTTP_HOST} ^www. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
# Stop hotlinking.
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} ^https?://([^/]+)/ [NC]
RewriteCond %1#%{HTTP_HOST} !^(.+)#1$
RewriteRule .(jpe?g|png|gif|swf|svg)$ - [NC,F,L]
# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript application/x-httpd-php
</ifmodule>
# END GZIP

最新更新