I'm looking for a set of simple rules and redirects for my site. I've tried so many combinations that I'm starting to get confused.
I'm not sure how to set this up. Generally, without mod-rewrite, I would use relative paths to link to files:
<a href="link.php">Link</a>
if it's in the same directory.
Now I'd like to use this:
<a href="link">Link</a>
And so if you go to this page:
localhost/mysite/link
it will take you to the correct place, which would be:
localhost/mysite/link.php
But also, many directory levels deep I would like it to work as well:
localhost/mysite/group/link2
would go to:
localhost/mysite/group/link2.php
and:
localhost/mysite/group/section/link3
would go to:
localhost/mysite/group/section/link3.php
But then in all these cases, if someone were to type in this:
localhost/mysite/group/section/link3.php
in the URL bar, it would show this:
localhost/mysite/group/section/link3
Thanks
EDIT
I think part of the problem I was having was that I had these directories in my site root:
product1
, product2
, product3
and I also had these files in a directory called products
:
product1.php
, product2.php
, product3.php
The .htaccess
file wasn't working until I changed these file names to something else, that was not the same as the directory names. Does this make sense? Is this expected behavior?
EDIT 2
I'm sure someone will come to this post looking for similar help as I did when I wrote this question. There are many good answers on mod_rewrite both here and on stackoverflow.com. I recommend reading through those and trying out these conditions and rules on a test instance to get the hang of them.
I realized also that using frequent browser cache clears and activating the RewriteLog is essential to figuring this stuff out. For example:
RewriteLog "rewrite.log" RewriteLogLevel 7
Have fun
You can do this by using .htaccess. The right code to achieve this is as follows: