Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been trying to wrap my head around .htaccess for most of the weekend, but I keep stumbling; I can get the server to work with one set of rules, but no more than one at a time.

My application is made up of several pages:

  • The Homepage pulls in content from the Detail page, (as well as content that the Detail page doesn't include), with an optional query string argument; e.g. ?s=item-name
  • The Detail page pulls in content with an optional query string argument (the same argument as the homepage; e.g. ?s=item-name - this would refer to exactly the same content)
  • A Collection page
  • A Related page

Each of these pages is a php file in the document root (index.php, detail.php, collection.php, related php).

What I would like to achieve:

  • The user should be able to go to or (so allow trailing slashes) instead of
  • If present, the query string argument (?s=item-name) should be entered after the page's trailing slash (so instead of; instead of This would be the case for all pages, so setting the rule on one page at a time seems rather cumbersome...

This is what my .htaccess file looks like at present, after much fiddling:

<IfModule mod_rewrite.c>  
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?s=$1 [L]

This allows the homepage to pull in the correct content, and I'm sure it's not too far off allowing any page to do the same, but I can't quite fathom it.

Can anyone help?

share|improve this question

You could extract the first part of the URL, and us it to test if it matches one of your php scripts, like this:

RewriteCond $1.php              -f
RewriteRule ^([^/]+)/(.*)       $1.php?s=$2
share|improve this answer
Not working I'm afraid... with or without the trailing slash, I get a file not found error. – MassivePenguin May 6 '13 at 12:52
My example was a suggestion that you could work from. In order to make it work in your case the best thing to do is enable rewritelog, so you can see what is going on. If you get a "file not found" it could be that it is rewriting... – Krist van Besien May 6 '13 at 14:42
up vote 0 down vote accepted

Someone was able to answer the question over at Stack Overflow:

share|improve this answer

Your Answer


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.