soubor .htaccess a nastaveni STRING
Ahoj kamarádi,
řeším problém s přesměrováním pomocí souboru .htaccess.
Jde mi o to, že pomocí redirectu chci zobrazovat url "hezky čitelně" narozdíl od staršího typu zobrazování, kde v url jsou paramtery.
Příklad:
www.domena.cz/produkt.php?id=1 »»»» www.domena.cz/produkt-alfa
www.domena.cz/produkt.php?id=2 »»»» www.domena.cz/produkt-beta
V souboru htaccess je pak:
Kód:
RewriteCond %{QUERY_STRING} id=1$
RewriteRule (.*) /produkt-alfa/? [R=301,L]
RewriteCond %{QUERY_STRING} id=2$
RewriteRule (.*) /produkt-beta/? [R=301,L]
Redirect takhle funguje a opravdu se url zobrazí dobře. Ovšem problém mám v tom, že na stránku tahám informace z databáze, a když v url není id=X tak se logicky z databaze nic nenačte...
Myslel jsem, že PHP se zapamatuje, že doména stále obsahuje ....php?id=1 tedy tahání z DB bude stále funkční, akorát htaccess url zobrazí v hezčím formátu.
Asi se tento problém řeší úplně jinak, ale to nevím jak.
Re: soubor .htaccess a nastaveni STRING
No jestli to chceš takto ponechat, tak ti chybí QSA v pravidlech (přidat do hranatých závorek). Ten příznak nastavuje keep query string (ponechej argumenty).
Ten otazník na konci url kam přesměrovávaš taky být nemusí (asi ani nesmí, nejspíše to zmrví, teď nemám čas to vyzkoušet).
Každopádně se stejně ve výsledku nevyhneš nějakému routeru přímo v PHP, aby uměl jak překládat tak vytvářet adresy. Takhle ti někdo/něco smázne ?id a produkt stejně nenajdeš.
Re: soubor .htaccess a nastaveni STRING
Ahoj Majku,
tak QSA sice přenese argumenty, ale těch jsem se právě chtěl zbavit, nicméně zajímavé je, že dopsáním toho QSA, mi to vždy vyhodilo nějakou chybu do smyčky.
A teď se toho u jednoho z případů (id=6) nemohu zbavit, ikdyž jsem celou informaci z htaccessu vymazal, stejně pokud zadám produkt.php?id=6 - redirectne mě to na /product-gama/?&&&&&&&&&&&&&&&&&&&&id=6
Zajímavé...