有没有更有效的方法来编码这个.htaccess文件?

时间:2010-09-29 22:13:55

标签: apache .htaccess

是否有更有效的方法来编码我在下面包含的.htaccess文件?此外,所有不同元素的顺序是否正常?一位用户将该文件描述为他见过的“最狂野”的htaccess,所以我想学习如何让它变得更好。

提前致谢!

<FilesMatch "(phpinfo|configurations).php$">
AuthName "Restricted Area" 
AuthType Basic 
AuthUserFile /web/example.com/library/ht/.htpasswd
AuthGroupFile /dev/null 
require valid-user
</FilesMatch>

IndexIgnore *

ErrorDocument 400 /index.php?module=error&action=error
ErrorDocument 401 /index.php?module=error&action=error
ErrorDocument 403 /index.php?module=error&action=error
ErrorDocument 404 /index.php?module=error&action=error
ErrorDocument 500 /index.php?module=error&action=error

RedirectMatch 301 ^/media/$ /
RedirectMatch 301 ^/media/documents/$ /
RedirectMatch 301 ^/media/graphics/$ /
RedirectMatch 301 ^/media/photos/$ /
RedirectMatch 301 ^/library/$ /
RedirectMatch 301 ^/library/css/$ /
RedirectMatch 301 ^/library/ht/$ /
RedirectMatch 301 ^/library/js/$ /
RedirectMatch 301 ^/library/php/$ /

RewriteEngine on
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]

RewriteRule ^home$ /index.php?module=home&action=frontpage
RewriteRule ^home/$ /index.php?module=home&action=frontpage
RewriteRule ^home/([^/\.]+)$ /index.php?module=home&action=$1
RewriteRule ^home/([^/\.]+)/$ /index.php?module=home&action=$1

RewriteRule ^cv$ /index.php?module=home&action=cv
RewriteRule ^cv/$ /index.php?module=home&action=cv

RewriteRule ^release$ /index.php?module=release&action=release
RewriteRule ^release/$ /index.php?module=release&action=release

RewriteRule ^photos$ /index.php?module=gallery&action=album&album=general
RewriteRule ^photos/$ /index.php?module=gallery&action=album&album=general

RewriteRule ^gallery$ /index.php?module=gallery&action=album&album=general
RewriteRule ^gallery/$ /index.php?module=gallery&action=album&album=general
RewriteRule ^gallery/([^/\.]+)$ /index.php?module=gallery&action=album&album=$1
RewriteRule ^gallery/([^/\.]+)/$ /index.php?module=gallery&action=album&album=$1
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)$ /index.php?module=gallery&action=album&album=$1$&page=$2
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=album&album=$1$&page=$2
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)$ /index.php?module=gallery&action=item&album=$1$&page=$2&item=$3
RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=item&album=$1$&page=$2&page=$3

RewriteRule ^handouts$ /index.php?module=home&action=handouts
RewriteRule ^handouts/$ /index.php?module=home&action=handouts

RewriteRule ^links$ /index.php?module=home&action=links
RewriteRule ^links/$ /index.php?module=home&action=links

RewriteRule ^contact$ /index.php?module=home&action=contact
RewriteRule ^contact/$ /index.php?module=home&action=contact

RewriteRule ^login$ /index.php?module=authentication&action=login
RewriteRule ^login/$ /index.php?module=authentication&action=login
RewriteRule ^logout$ /index.php?module=authentication&action=logout
RewriteRule ^logout/$ /index.php?module=authentication&action=logout

RewriteRule ^copyright$ /index.php?module=home&action=copyright
RewriteRule ^copyright/$ /index.php?module=home&action=copyright

RewriteRule ^error$ /index.php?module=error&action=error
RewriteRule ^error/$ /index.php?module=error&action=error

<FilesMatch "(phpinfo|configurations).php$"> AuthName "Restricted Area" AuthType Basic AuthUserFile /web/example.com/library/ht/.htpasswd AuthGroupFile /dev/null require valid-user </FilesMatch> IndexIgnore * ErrorDocument 400 /index.php?module=error&action=error ErrorDocument 401 /index.php?module=error&action=error ErrorDocument 403 /index.php?module=error&action=error ErrorDocument 404 /index.php?module=error&action=error ErrorDocument 500 /index.php?module=error&action=error RedirectMatch 301 ^/media/$ / RedirectMatch 301 ^/media/documents/$ / RedirectMatch 301 ^/media/graphics/$ / RedirectMatch 301 ^/media/photos/$ / RedirectMatch 301 ^/library/$ / RedirectMatch 301 ^/library/css/$ / RedirectMatch 301 ^/library/ht/$ / RedirectMatch 301 ^/library/js/$ / RedirectMatch 301 ^/library/php/$ / RewriteEngine on RewriteBase / RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/ RewriteRule ^index\.php$ / [R=301,L] RewriteRule ^home$ /index.php?module=home&action=frontpage RewriteRule ^home/$ /index.php?module=home&action=frontpage RewriteRule ^home/([^/\.]+)$ /index.php?module=home&action=$1 RewriteRule ^home/([^/\.]+)/$ /index.php?module=home&action=$1 RewriteRule ^cv$ /index.php?module=home&action=cv RewriteRule ^cv/$ /index.php?module=home&action=cv RewriteRule ^release$ /index.php?module=release&action=release RewriteRule ^release/$ /index.php?module=release&action=release RewriteRule ^photos$ /index.php?module=gallery&action=album&album=general RewriteRule ^photos/$ /index.php?module=gallery&action=album&album=general RewriteRule ^gallery$ /index.php?module=gallery&action=album&album=general RewriteRule ^gallery/$ /index.php?module=gallery&action=album&album=general RewriteRule ^gallery/([^/\.]+)$ /index.php?module=gallery&action=album&album=$1 RewriteRule ^gallery/([^/\.]+)/$ /index.php?module=gallery&action=album&album=$1 RewriteRule ^gallery/([^/\.]+)/([^/\.]+)$ /index.php?module=gallery&action=album&album=$1$&page=$2 RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=album&album=$1$&page=$2 RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)$ /index.php?module=gallery&action=item&album=$1$&page=$2&item=$3 RewriteRule ^gallery/([^/\.]+)/([^/\.]+)/([^/\.]+)/$ /index.php?module=gallery&action=item&album=$1$&page=$2&page=$3 RewriteRule ^handouts$ /index.php?module=home&action=handouts RewriteRule ^handouts/$ /index.php?module=home&action=handouts RewriteRule ^links$ /index.php?module=home&action=links RewriteRule ^links/$ /index.php?module=home&action=links RewriteRule ^contact$ /index.php?module=home&action=contact RewriteRule ^contact/$ /index.php?module=home&action=contact RewriteRule ^login$ /index.php?module=authentication&action=login RewriteRule ^login/$ /index.php?module=authentication&action=login RewriteRule ^logout$ /index.php?module=authentication&action=logout RewriteRule ^logout/$ /index.php?module=authentication&action=logout RewriteRule ^copyright$ /index.php?module=home&action=copyright RewriteRule ^copyright/$ /index.php?module=home&action=copyright RewriteRule ^error$ /index.php?module=error&action=error RewriteRule ^error/$ /index.php?module=error&action=error

2 个答案:

答案 0 :(得分:1)

我现在看到的第一件事就是你对“家”和“家/”之类的东西有一个单独的规则,两者都去了同一个地方。为了解决这个问题,请替换

RewriteRule ^home$ /index.php?module=home&action=frontpage
RewriteRule ^home/$ /index.php?module=home&action=frontpage

RewriteRule   ^home/?$   /index.php?module=home&action=frontpage

重复所有这些重复项,你将把规则数量减少一半。

您还可以将一些规则组合在一起,使用相同模块的规则,并一次重写所有规则。例如,您可以替换

RewriteRule ^handouts/?$ /index.php?module=home&action=handouts
RewriteRule ^links/?$ /index.php?module=home&action=links
RewriteRule ^contact/?$ /index.php?module=home&action=contact
RewriteRule ^copyright/?$ /index.php?module=home&action=copyright
RewriteRule ^cv/?$ /index.php?module=home&action=cv

RewriteRule ^login/?$ /index.php?module=authentication&action=login
RewriteRule ^logout/?$ /index.php?module=authentication&action=logout

RewriteRule ^(handouts|links|contact|copyright|cv)/?$   /index.php?module=home&action=$1
RewriteRule ^(log(in|out))/?$   /index.php?module=authentication&action=$1

您可以使用所有RedirectMatch规则执行类似操作。如果您的目标是阻止人们浏览您的内容,您可以禁用目录浏览,将“无索引”页面设置为/,或者(如果您希望将每个可能的索引退回到/):

RedirectMatch 301   ^/(media|library)/([^/]+/)?$    /

理想情况下,如果你可以将它们放在那里,你需要在你网站的实际配置文件中使用这些规则中的大部分。 (.htaccess重写可能会非常慢,因为Apache必须采取一些神奇的措施才能实现它们。)但是如果你的网络主机不会让你这样做(很多人不会),那么你可能只需处理用。

答案 1 :(得分:0)

您可以按模块组合一些规则并组合两行:有和没有结尾/作为/?

RewriteRule ^(handouts|links|contact|copyright|cv)/?$ /index.php?module=home&action=$1
RewriteRule ^(login|logout)/?$ /index.php?module=authentication&action=$1
RewriteRule ^error/?$ /index.php?module=error&action=error
RewriteRule ^release/?$ /index.php?module=release&action=release