Я знаю, как переписать URL, например:
www.example.com/index.php?id=1&cat=3
до www.example.com/1/3/
(или любой другой). Я знаю это.
Чего я не знаю, так это того, как вообще изменить мои ссылки на всех страницах, чтобы они ссылались на красивые URL. Все ссылки на моем сайте устарели (
), и их много.
Я спрашиваю, есть ли у кого-нибудь идеи или сведения как автоматически перенаправить на этот красивый URL-адрес, если пользователь щелкнет index.php? id = 1. (Почти как этот сайт Stackoverflow, если вы измените заголовок в URL).
Итак, я предполагаю ...
Используйте .htaccess, чтобы прочитать index.php? Id = 1 & cat = 2, чтобы переписать index / 1/3 который сам снова интерпретирует (странно)
файл php для выполнения перенаправлений, которые htaccess перезаписывает обратно в исходное состояние ...
Заключение: изменение
автоматически в index / 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]