Pergunta

Eu tenho um arquivo htaccess que começa com as coisas regulares:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Então tem algumas reescritas, por exemplo:

RewriteRule ^protect/?$ /Legal/Your-Financial-Protection.aspx   [NC, L, R=301]

Em seguida, termina com um reescrita:

RewriteMap map txt:rewritemaps/map.txt  [NC]
RewriteRule ^(.+)$  ${map:$1}   [R=301]

O rewritemap contém todos os nossos URLs herdados - páginas em nosso site atual e URLs curtos que serão redirecionados para páginas equivalentes no novo site (existem cerca de 4K, então preciso usar um mapa):

Inspiration.aspx /Inspiration.aspx
Destinations/Africa/Countries/Gabon.aspx /Destinations/Africa/Gabon.aspx
indonesia /Destinations/Southeast-Asia/Indonesia.aspx

O problema é que, com o rewritemap ativado (ou seja, não comentou), todos os meus URLs (mesmo aqueles que não correspondem) redirecionam para / - incluindo folhas de estilo, js, imagens etc.

O que estou procurando é Uris que corresponde ao padrão no mapa para redirecionar para a substituição e tudo o mais para passar (ou seja, permaneça o mesmo).

Tentei definir um padrão de US $ 1 no mapa:

RewriteRule ^(.+)$  ${map:$1|$1}    [R=301]

Mas isso apenas causa um loop de redirecionamento. Eu acho que também poderia pular a reescrita para todos .css, .js, .jpg etc, mas prefere não ter que manter uma lista de extensões de arquivo usadas.

Para sua informação, estou usando o IsaaPirewrite da Helicontech (como estou no IIS 6), embora afirme lidar com o reescrita de acordo com o Apache e nunca tivemos problemas no passado.

Qualquer ajuda seria grandemente agradecida!

Obrigado, Adam

Foi útil?

Solução

Just put only the pairs in the map that actually differ. Otherwise you will redirect to the same value and get an infinite recursion.

And to redirect only when a match is found, try this:

RewriteCond ${map:$1} ^/.+
RewriteRule ^(.+)$ %0 [R=301]

Outras dicas

This line is the problem:

RewriteMap map txt:rewritemaps/map.txt  [NC]

You cannot define a RewriteMap in a .htaccess file. (See RewriteMap docs) You must define the map in the server or virtualhost context, and then it can be referenced in your .htaccess file. If you turn up the log level high enough, you'll see a warning that your map is not being loaded.

Gumbo's solution is great, except that it doesn't seem to handle URL's with spaces. http://www.webmasterworld.com/apache/3830228.htm offers insight into how to handle spaces, combining the two like so:

  RewriteCond ${moved:${escape:$1}} ^/.+
  RewriteRule ^(.+)$ %0 [R=301]

Doesn't work. Damn I hate mod_rewrite

  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (2) init rewrite engine with requested uri /press/Partners With City.pdf
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (3) applying pattern '^(.+)$' to uri '/press/Partners With City.pdf'
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (5) map lookup OK: map=escape key=/press/Partners With City.pdf -> val=/press/Partners%20With%20City.pdf
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (6) cache lookup FAILED, forcing new map lookup
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (5) map lookup FAILED: map=moved[txt] key=/press/Partners%20With%20City.pdf
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (4) RewriteCond: input='' pattern='^/.+' => not-matched

adam, Gumbo's rules includes the leading slash in the key, and the posted version of your map is missing them.

RewriteLogLevel 9 is very handy for uncovering things such as this.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top