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 currently use a RewriteRule to direct all traffic to a index.php file which then decides which page to serve. The current rule works fine and looks like this:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

This means if I go to the following URL, the correct page will be displayed:

My problem:

I'd like to be able to add the link /teaching-and-training/20 to my page which should redirect to the above URL, i.e. /main/index/1/basic/0/0/date/14/0/0/20/0/0/0/0/0/0/0

I can get this to work if I use the following RewriteRule:

RewriteRule ^teaching-and-training/([0-9]+)$$1/0/0/0/0/0/0/0 [L]

but it does not work if I remove the part. I need to remove this part.

EDIT: By "does not work" I mean the redirection is successful, however the URL is changed to the ugly, long URL. I would like the URL to remain as teaching-and-training/20.

I suspect the problem is that main/index/1/basic/0/0/date/14/0/0/$1/0/0/0/0/0/0/0 doesn't really exist (the index.php file is able to figure out which page this is), so my question is this:

Is there some way I can make ^teaching-and-training/([0-9]+)$ display /main/index/1/basic/0/0/date/14/0/0/$1/0/0/0/0/0/0/0 by either using a little bit more RewriteRule magic, or by combining it with the index.php RewriteRule?

Any advice appreciated.


share|improve this question

If you want teaching-and-training/([0-9]+) to display that long URL, then you should place the rule first (i.e. near the top of the file) and not use [L]. This way, mod_write will rewrite it once, and then again when it hits your normal rule. The [L] means "this rule is last, if it matches, run with what I've just rewritten you to" which is the opposite of what you wanted.

RewriteRule ^teaching-and-training/([0-9]+)$ /main/index/1/basic/0/0/date/14/0/0/$1/0/0/0/0/0/0/0
share|improve this answer
Thank you for your reply. I have tried those changes and it gives a 500 error. From the logs I can see the following: RewriteRule: bad flag delimiters. – Steve Dec 9 '12 at 10:52
I've tried your updated rule and it works... however, it is changing the URL from /teaching-and-training/20 to the long, ugly URL. Is there a way to keep the URL as /teaching-and-training/20? Thanks – Steve Dec 9 '12 at 11:04
Ah I misunderstood what you wanted, sorry. Please see my revised answer. – Jay Dec 9 '12 at 11:07
Please don't apologise, I should have explained better. :) I have tried your suggestion but it gives a 404 error. This is my htaccess: RewriteRule ^teaching-and-training/([0-9]+)$ /main/index/1/basic/0/0/date/14/0/0/$1/0/0/0/0/0/0/0 RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] – Steve Dec 9 '12 at 11:17
You probably need to tinker with it some more. Try making it main/ instead of /main, etc. Difficult for me to see what the problem is from here, but it sounds like it's being re-written incorrectly (but we are getting somewhere nonetheless). You might also like to try RewriteBase / right after RewriteEngine On. mod_rewrite is a funny beast. – Jay Dec 9 '12 at 11:36

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.