Nicolas Nicolas - 1 year ago 92
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.



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
link is not working but

And for some reason if I want to open the link
its redirecting me to:
but If I capitalize the
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)


Just to clear things up I want
to redirect to my
should be redirected to
. I tried adding a question mark after the forward slash in my
and I also tried putting the
rule above the
rule. Nothing worked yet.


I updated my
code above and now nearly everything works. The only thing that still doesn't work is when I open
, my resources aren't loading!

Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download