Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
    
you can do it like this: RewriteRule ^(.*)$ $1/$1.php which means "product2/product2.php" –  Hex Oct 29 '12 at 23:26
    
Not quite sure that's what I meant... All the PHP files for products are in the products directory. But I also have directories that happen to be the same strings as the PHP files: products/product1.php' and 'product1 which has other files, like eval-key.php, documentation.php, etc. I tested it again, and, sure enough, the mod_rewrite rules fail when I have a product1 directory and a file called `product1.php'. –  nicorellius Oct 30 '12 at 0:01

1 Answer 1

up vote 2 down vote accepted

You can do this by using .htaccess. The right code to achieve this is as follows:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.*)$ $1.php

share|improve this answer
    
I just made an edit to my question that I could use assistance with. Your answer did work, so I will accept it as correct. –  nicorellius Oct 29 '12 at 23:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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