R=301, htaccess redirect to the new page

Asked

Viewed 37 times

1

Well I created a page with the custom URL

RewriteRule     ^produtos/([A-Za-z0-9-]+)/?$    produto.php?product=$1      [NC,L]  

good, up to there ok, the new link works

But I’m trying to block direct access through the URL produto.php?product=$1

and if by chance the person tries, be redirected to the New URL that in the case is produtos/?NOMEQUEVAISERMANDADO

1 answer

0

You simply have to create a new rule if the requested URL corresponds to produto.php:

RewriteEngine On
RewriteBase /

# Redirecionar se acessar diretamente para produto.php
#  a) com o parâmetro ?product=xxxxxx
RewriteCond %{QUERY_STRING}              ^(?:.*&)?product=([a-z0-9-]*)  [NC]
RewriteRule ^produto\.php/?$             produtos/%1  [NC,QSD,R,L]
#  b) sem o parâmetro
RewriteRule ^produto\.php/?$             produtos/    [NC,R,L]

# Reescrita normal
RewriteRule ^produtos/([a-z0-9-]*)/?$    produto.php?product=$1  [NC,QSA,END]


  • The parameter ?product=xxxx can only be captured in a Rewritecond. %1 refers to group 1 in the same way as $1 is used in a Rewriterule.
  • Make sure to set the flag [END] in the rewrite, to avoid infinite redirects.


I clicked on a free lodging if you want to test it:

  1. http://mariano.uphero.com/259605/produtos/nome-produto
  2. http://mariano.uphero.com/259605/produto.php?product=NOMEQUEVAISERMANDADO
  3. http://mariano.uphero.com/259605/produto.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.