开发者

Is there a more efficient way to code this .htaccess file?

Is there a more efficient way to code the .htaccess file I have included below? Also, is the order of all the different elements ok? One user described the file as the "bushiest" htaccess he had ever seen, so I want to learn how I can make it better.

Thanks in advance!

<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


First thing i see right off, is that you have a separate rule for things like "home" and "home/", both of which go to the same place. In order to fix that, replace

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

with

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

Repeat for all such duplicates, and you'll cut the number of rules almost in half.

You could also group some of the rules together, ones that use the same module, and rewrite all of them at once. For example, you could replace

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

with

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

You could do something similar with all your RedirectMatch rules. If your goal is to keep people from browsing through your stuff, you could either disable directory browsing, set your "no index" page to /, or (if you want to bounce every would-be index to /):

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

Ideally, you'd want most of these rules in the actual config files for your site, if you can get them put there. (.htaccess rewrites can be quite slow, due to some magic Apache has to do to implement them.) But if your web host won't let you do that (and many won't), that's something you might just have to deal with.


You can combine some rules by modules and combine 2 lines: with and without ending / as /?

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜