stepik21
stepik21

Reputation: 2640

php - Weird htaccess mod rewrite acting

I'm trying to set "nice urls" to my website and I have this structure of mod rewrite conditions:

RewriteRule ^/$ /index.php?&%{QUERY_STRING} [L]
RewriteRule ^tabulky-velikosti/?$ tabulky-velikosti.php?&%{QUERY_STRING} [L]
RewriteRule ^o-bambusu/?$ o-bambusu.php?&%{QUERY_STRING} [L]
RewriteRule ^kolekce/?$ kolekce.php?&%{QUERY_STRING} [L]
RewriteRule ^vymena-zbozi/?$ vymena-zbozi.php?&%{QUERY_STRING} [L]
RewriteRule ^doprava-a-platba/?$ doprava-a-platba.php?&%{QUERY_STRING} [L]
RewriteRule ^obchodni-podminky/?$ obchodni-podminky.php?&%{QUERY_STRING} [L]
RewriteRule ^ochrana-osobnich-udaju/?$ ochrana-osobnich-udaju.php?&%{QUERY_STRING} [L]
RewriteRule ^onas/?$ onas.php?&%{QUERY_STRING} [L]
RewriteRule ^contact/?$ kontakt.php?&%{QUERY_STRING} [L]
RewriteRule ^faq/?$ faq.php?&%{QUERY_STRING} [L]

RewriteRule ^kategorie/panske-pradlo/?$ category.php?gender=panske&%{QUERY_STRING} [L]
RewriteRule ^kategorie/damske-pradlo/?$ category.php?gender=damske&%{QUERY_STRING} [L]
RewriteRule ^kategorie/detske-pradlo/?$ category.php?gender=detske&%{QUERY_STRING} [L]

RewriteRule ^(.*)/?$ product.php?url=$1&%{QUERY_STRING} [L]

When I use testing tool (https://htaccess.madewithlove.be), all is good, but when I try to run this on actual website, there is problem with last condition

RewriteRule ^(.*)/?$ product.php?url=$1&%{QUERY_STRING} [L]

It seems that all conditons above are being ignored, for example when I ask for url www.domain.com/contact/, it should show www.domain.com/kontakt.php, but instead of that, it shows page www.domain.com/product.php?url=contact.

When I remove the last line, everything is working as it should.

Can anyone give me some solution to this please? Thanks alot!

Upvotes: 0

Views: 28

Answers (1)

anubhava
anubhava

Reputation: 785266

.* matches everything and you don't have any RewriteCond above last rule to make that rule conditional.

Besides you have lot of unnecessary use of %{QUERY_STRING} in target URI that you can get with QSA flag.

Fully refactored .htaccess should be like this:

DirectoryIndex index.php
RewriteEngine On

RewriteRule ^tabulky-velikosti/?$ tabulky-velikosti.php [L]
RewriteRule ^o-bambusu/?$ o-bambusu.php [L]
RewriteRule ^kolekce/?$ kolekce.php [L]
RewriteRule ^vymena-zbozi/?$ vymena-zbozi.php [L]
RewriteRule ^doprava-a-platba/?$ doprava-a-platba.php [L]
RewriteRule ^obchodni-podminky/?$ obchodni-podminky.php [L]
RewriteRule ^ochrana-osobnich-udaju/?$ ochrana-osobnich-udaju.php [L]
RewriteRule ^onas/?$ onas.php [L]
RewriteRule ^contact/?$ kontakt.php [L]
RewriteRule ^faq/?$ faq.php [L]

RewriteRule ^kategorie/((?:pan|dam|det)ske)-pradlo/?$ category.php?gender=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ product.php?url=$1 [L,QSA]

Take note of RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d before last rule to prevent execution for valid files and directories.

Upvotes: 2

Related Questions