1
votes

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

3 Answers

0
votes

(.*) 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.

0
votes

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.

0
votes
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.