Rewrite url with mod_rewrite and .htaccess

1

I have a URL on my website: https://www.miweb.com/componente/nombre/contratar

I would like it to not show "name" because the company has changed, but that the URL continues to lead to the same place, because if the contract does not work.

Is it possible to show the user a URL but that it is pointing to the real one?

I've done it this way in the .htaccess file with mod_rewrite, but it just redirects me and of course, error 404 comes out.

RewriteEngine On
Rewritebase /

<IfModule mod_rewrite.c>
    RewriteRule ^componente/nombre/contratar$ http://%{HTTP_HOST}/componente/contratar$1 [R=302,L]
</IfModule>

How can I solve it?

    
asked by Norak 13.03.2018 в 09:31
source

1 answer

2

What you did is perfect as a first step. You just have to add another rule. What you're looking for is a rewrite : that Apache modifies the URL but that none of this goes to the user as a redirect.

RewriteEngine On
Rewritebase /

# Tu regla para redireccionar (con algunos detalles modificados)
RewriteRule ^componente/nombre/contratar(/.*|$) componente/contratar$1 [NC,R=302,L]

# Reescribir de  /componente/contratar/*  a  /componente/nombre/contratar/*
RewriteRule ^componente/contratar(/.*|$) componente/nombre/contratar$1 [NC,END]
  • The key in this last redirect is to use [END] to stop following trying to apply other rules and do not fall into an infinite loop between redirection and rewrite.
  • I uploaded a demo to a free hosting that you can try at link
answered by 13.03.2018 / 11:00
source