PHP .htaccess -> URL bonita (en reversa)
Sé cómo reescribir URL, por ejemplo:www.example.com/index.php?id=1&cat=3
awww.example.com/1/3/
(o lo que sea). Yo sé eso
Lo que no sé es cómo cambiar mis enlaces completos en todas las páginas para vincularlos a URL bonitas. Todos los enlaces de mi sitio son anticuados <a href="index.php?id=1&cat=2">
) y hay muchos.
Estoy preguntando si alguien tiene una idea o sabe cómo redirigir automáticamente a esa bonita url si el usuario hace clic en index.php? Id = 1. (Casi como este sitio Stackoverflow si cambia el título en la URL).
Así que mis presunciones son ...
Utilice .htaccess para leer index.php? Id = 1 & cat = 2 para reescribir index / 1/3 que se interpreta de nuevo (extraño)
un archivo php para hacer los redireccionamientos que htaccess reescribe al original ...
Conclusión: cambio<a href="index.php?id=1&.....">
automáticamente aindex/1/2
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
##################################
# This turns index.php?id=1&cat=2 into index/1/2 and then back 'transparent' into index.php?id=1&cat=2 if you have old fashioned
# links in your site and don't want to change them :)
# Avoid mod_rewrite infinite loops
# This is critical if you want to use this code
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
# Hard-rewrite ("[R]") to "friendly" URL.
# Needs RewriteCond to match original querystring.
# Uses "?" in target to remove original querystring,
# and "%n" backrefs to move its components.
# Target must be a full path as it's a hard-rewrite.
RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$
RewriteRule ^index.php$ http://localhost/index/%1/%2/? [L,R]
# Soft-rewrite from "friendly" URL to "real" URL.
# Transparent to browser.
# Won't re-trigger the above rewrite, though I'm
# not really sure why! The order of the rules
# doesn't seem to make a difference.
RewriteRule ^index/(\d+)/(\d+)/$ index.php?id=$1&cat=$2 [L]