Nicolas Nicolas - 3 months ago 20
Apache Configuration Question

htaccess rewrite subdirectoy to file

I have a problem with rewriting urls to my files. What I am trying to do is making my little shop system a bit more SEO friendly. My problem is that it sometimes works and sometimes it doesn't. I have no idea what I should do or what I am doing wrong.

My UPDATED

.htaccess
file:

ErrorDocument 404 /shop/404.html

RewriteEngine On
RewriteBase /shop/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^produkte/?(.*)$ products.php$1 [L]

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

RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]


For example the
/shop/products
link is not working but
/shop/products/
is.

And for some reason if I want to open the link
/shop/products?cat=besteck
its redirecting me to:
localhost/D:/xampp/htdocs/shop/products.php?cat=besteck
but If I capitalize the
b
it's working fine..

I have no Idea what to do, please help me! (And dont just give a working code snippet explain why mine fails and yours works)

EDIT:



Just to clear things up I want
/products
,
/products/
and
/products?some_get_query
to redirect to my
products.php
file.
/product/some_seo_url
should be redirected to
product.php?url=some_seo_url
. I tried adding a question mark after the forward slash in my
RewriteRule
and I also tried putting the
^products
rule above the
^product
rule. Nothing worked yet.

EDIT 2:



I updated my
.htaccess
code above and now nearly everything works. The only thing that still doesn't work is when I open
/shop/products/?cat=fish
or
/shop/product/some_product
, my resources aren't loading!

Answer
ErrorDocument 404 /shop/404.html

RewriteEngine On
RewriteBase /shop/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^products/?(.*)$ products.php?$1 [L]

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

RewriteRule ^(.*)/(css|js|img|fonts)/(.*)?$ /shop/$2/$3 [L,QSA,R=301]

First off your product rule also matches products so a rewrite like:

products/cat/fish becomes product.php?url=s/cat/fish

Which is not what you want, the easiest way to avoid that is to reverse the order so that the products rewrite comes before the product one but I've also added the Last flag ([L]) to be on the safe side; besides, once it's got the match you want it's better for it to stop looking.

To prevent recursive rewrite loops you need to specify that the rewrite only occurs when the redirect is not an existing file or directory (otherwise your product rewrite matches product.php and it loops - forever). That's what those RewriteCond lines signify.

Other than that it seems OK.

Comments