R = 301, htaccess redirect to new page

1

Well I created a page with the custom URL

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

Okay, so good, the new link works

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

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

    
asked by anonymous 01.12.2017 / 01:06

1 answer

0

You simply have to create a new rule if the requested URL matches 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 ?product=xxxx parameter can only be captured in a RewriteCond. %1 refers to group 1, just as $1 is used in a RewriteRule.
  • Be sure to set the [END] flag in the rewrite, to avoid infinite redirects.


I uploaded a free hosting if you want to test it:

  • link
  • link
  • link
  • 01.12.2017 / 11:38