Eventhough I'm new at htaccess, I've been learning a lot in the past few days, but I'm stuck on a certain part that resulted in the website not behaving as desired. Here is the part of the code where I have my delima:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
Rewriterule ^ %{REQUEST_FILENAME}.php [NC]
# RewriteCond /templates/%{REQUEST_FILENAME}.html -f
RewriteRule ^([^/\.]+)$ index.php?$1 [NC,S=1]
RewriteRule ^([a-zA-Z0-9]+)$ product.php?product_id=$1 [NC]

The first RewriteCond and RewriteRule works perfect. My problem with what goes afterwards.

What I need to achieve is that when a user inputs /some-word it would search if a file with the same name is valid in the /templates folder, and hence would serve it as index.php?some-word, else it would be served as product.php?product_id=some-word. The the index.php rule works well when the second condition is commented out, but when added only the product.php rule works.

The supplied "tag" could be numbers, letters, hyphenated words (i.e. any-words), or a combination of numbers and letters. The rules should be case insensitive.

I appreciate any assistance on the matter.

  • You need a condition for the first and the second rules as well, otherwise only the last one will be applied. – Marcel Oct 9 '15 at 13:02
  • Thanks Marcel, but that is what I did, the first condition worked with the first rule, but the second condition will not work with the second rule, for some reason. I might have done something wrong with the second condition, any ideas? – elrayyes Oct 9 '15 at 13:44

The problem is that %{REQUEST_FILENAME} is the absolute path translated from the URL, that is, for http://example.com/foobar it may be something like /var/www/vhost/example.com/httpdocs/foobar (depending on how your web server is setup). I suggest you use pattern matching on the URL as in

RewriteCond %{DOCUMENT_ROOT}/templates/$1.html -f
RewriteRule ^([^\/\.]+)$ index.php?$1 [NC,S=1]

(I didn't test that though)

  • Thanks Hagen, that was perfect, worked like a charm. I know it was something with my second condition. Again, thank you. – elrayyes Oct 9 '15 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.