Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

422
Visualizações
How to capture and log any 301 redirects (.htaccess) that happen using PHP

I have a bunch of 301 redirects set in my .htaccess file. What I'd like to do is keep track of how many times a redirect is used, so that I can manage them better.

Is there a way to capture and log 301 redirects matches that happen with PHP?

In other words, if the redirect is:

RewriteRule ^about/bozo$ /about [R=301,L]

Is there a way to capture the "about/bozo" every time it happens using PHP? (even though the redirects are handled by htaccess calls, and not by PHP header redirects)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You cannot log a 3xx redirect that is triggered earlier in the request by Apache (eg. in .htaccess) using PHP. The redirect is entirely invisible to PHP.

You can't use the HTTP Referer to determine if a redirect occurred since the browser preserves the Referer from the initial request (if any).

Requests that trigger a 3xx redirect should be logged in the server's access log.

The only way you could do this with PHP is if you mark the redirected URL in some way (perhaps with a query string) and check for this in your PHP script. For example:

RewriteRule ^about/bozo$ /about?redirected=true [R=301,L]

... but that maybe undesirable.

over 4 years ago · Santiago Trujillo Relatório

0

MrWhite's answer is correct that you can't see those redirects in PHP. However, you don't need to use PHP to get the stats you want.

If you want stats on how often those redirects happen, you can get them from your web server's access_log file. Every 301 redirect should add an entry in the log file regardless of whether the redirect was triggered by .htaccess or PHP. Such a log entry should look something like this:

127.0.0.1 - - [01/Jan/2000:11:55:36 -0700] "GET /about/bozo HTTP/1.1" 301 456

You can use command line tools to count redirects:

grep '" 301 ' /var/log/apache2/access_log | wc -l

or

grep 'GET /about/bozo ' /var/log/apache2/access_log | wc -l

Where grep does the filtering of the log file and wc -l counts the lines.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda