Есть ли более эффективный способ кодирования этого файла .htaccess? - PullRequest
1 голос
/ 30 сентября 2010

Есть ли более эффективный способ кодирования файла .htaccess, который я включил ниже? Кроме того, порядок всех различных элементов в порядке? Один пользователь назвал этот файл «самым кустарным», который он когда-либо видел, поэтому я хочу узнать, как его улучшить.

Заранее спасибо!

</p> <pre><code><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 ]

1 голос
/ 30 сентября 2010

Первое, что я сразу вижу, это то, что у вас есть отдельное правило для таких вещей, как «дом» и «дом /», оба из которых идут в одно и то же место. Чтобы это исправить, замените

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 должен сделать для их реализации.) Но если ваш веб-хостинг не позволит вам сделать это (а многие этого не сделают), это то, что вам, возможно, просто придется иметь дело с.

0 голосов
/ 30 сентября 2010

Вы можете объединить некоторые правила по модулям и объединить 2 строки: с и без окончания / 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...