1

This problem has been bugging me for a while now.

I have a created a small site engine and I'm using mod_rewrite to tell the engine what page to proccess, SEO friendly links is a bonus :).

This is how it's works today:

the adress http://www.example.com/site/page becomes http://www.example.com/engine.php?address=page

But what i want is: the adress http://www.example.com/page becomes http://www.example.com/engine.php?address=page

Everything works fine if i create a psuedo directory for the calls (/site) but when i try to do the same from the root strange things start to happends.

RewriteBase /
RewriteRule ^site/(.*) engine.php?%{QUERY_STRING}&address=$1

Works fine: /site/about/contacts becomes eninge.php?address=about/contacts

RewriteBase /
RewriteRule ^(.*)$ eninge.php?%{QUERY_STRING}&address=$1

Doesn't work, for some reason /about/contacts becomes eninge.php?address=eninge.php

3 Answers 3

0

(.*) means catch anything. Have you tried exluding files and directory before your catch-all ? Because it will cause an infinite recursion without it.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ eninge.php?%{QUERY_STRING}&address=$1 [L]

More information is available in the official documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Update: You should also specify [L] at the end of your rule, to tell Apache to end the rewriting process here.

3
  • That did it :), It was the [L] flag who was missing :). Cheers.
    – ztripez
    Commented Apr 16, 2012 at 14:18
  • Works fine, however. I also want to get www.example.com/ (and www.example.com) to redirect to example.com/engine.php?address=index
    – ztripez
    Commented Apr 16, 2012 at 14:43
  • If you want to keep engine.php, just add append it to your DirectoryIndex file list. Commented Apr 16, 2012 at 15:09
0

Check the RewriteLog (this has been updated in 2.4, check current docs if not using 2.2):

RewriteLog "/usr/local/var/apache/logs/rewrite.log"
RewriteLogLevel 3 

This will show you exactly what mod_rewrite is doing and allow you to tune your configuration based on its output. Beware - it grows very quickly, and should never be used in production environments.

As an aside, you have some typos in your post - worth verifying that these differ from your config.

1
  • RewriteLog, I've been looking for something like that. Thanks. Corrected some typos :)
    – ztripez
    Commented Apr 16, 2012 at 14:07
0
RewriteCond %{REQUEST_FILENAME} !^engine.php
RewriteRule (.*) engine.php?address=$1 [QSA,L]

Try this. What you have is causing the rewrite to loop around and first do engine.php?address=about/contacts as you were expecting, but then go around again and rewrite that to engine.php?address=engine.php. Make sense? The [QSA,L] is a Query String Append and Last flag that will add the query string to your URL and tell the rewrite engine to stop looking for rewrites. The RewriteCond %{REQUEST_FILENAME} !^engine.php is to check that you haven't already specified the engine rewrite by ensuring the current URL doesn't start with engine.php. This is necessary if you are writing this in an .htaccess file rather than the .httpd config files.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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