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

In Apache HTTPD 2.2, I want to make it so requests for /index.foo or /foo, if they aren't files or directories, become /index.php?transform=foo, regardless of how many levels the URL has.

Criteria for success would be:

  • http://www.site.example/index.foo -> http://www.site.example/index.php?transform=foo
  • http://www.site.example/foo -> http://www.site.example/index.php?transform=foo
  • http://www.site.example/bar/baz/foo -> http://www.site.example/bar/baz/index.php?transform=foo

I.e., I don't want rewrites to be relative to the .htaccess directory; I want them to be relative to the topmost directory in the request.

share|improve this question

2 Answers 2

# Check if file exists
RewriteCond %{REQUEST_FILENAME} ! -f

# Rewrite the URI if file doesn't exist and contains "index."
RewriteRule ^(.*)(index\.[^/]+)$ $1/index.php?transform=$2 [L]

# The same thing for any other URI
RewriteCond %{REQUEST_FILENAME} ! -f
RewriteRule ^(.*)([^/]+)$ $1/index.php?transform=$2 [L]
share|improve this answer
    
Reaults in a 500 Error. –  Hugh Guiney Nov 7 '10 at 21:33
    
Check your error log and activate RewriteLog to find out what causes the error. –  joschi Nov 8 '10 at 6:24
    
At first it said "bad flag delimiters". I deleted the space after the exclamation points and that allowed me to access the site root, but as soon as I tried to access /foo I got another 500, with this in the log: "Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace." –  Hugh Guiney Nov 10 '10 at 2:14
    
There's a ton in the rewrite log but here's the first few lines (with sensitive info replaced), "foo" in this case being "html": 127.0.0.1 - - [09/Nov/2010:21:14:31 --0500] [site.example/sid#1008e2800][rid#1009b7e98/initial] (3) [perdir /Users/user/Sites/site/] strip per-dir prefix: /Users/user/Sites/site/html -> html 127.0.0.1 - - [09/Nov/2010:21:14:31 --0500] [site.example/sid#1008e2800][rid#1009b7e98/initial] (3) [perdir /Users/user/Sites/site/] applying pattern '^(.*)(index\.[^/]+)$' to uri 'html' –  Hugh Guiney Nov 10 '10 at 2:20
    
127.0.0.1 - - [09/Nov/2010:21:14:31 --0500] [site.example/sid#1008e2800][rid#1009b7e98/initial] (3) [perdir /Users/user/Sites/site/] strip per-dir prefix: /Users/user/Sites/site/html -> html 127.0.0.1 - - [09/Nov/2010:21:14:31 --0500] [site.example/sid#1008e2800][rid#1009b7e98/initial] (3) [perdir /Users/user/Sites/site/] applying pattern '^(.*)([^/]+)$' to uri 'html' 127.0.0.1 - - [09/Nov/2010:21:14:31 --0500] [site.example/sid#1008e2800][rid#1009b7e98/initial] (4) [perdir /Users/user/Sites/site/] RewriteCond: input='/Users/user/Sites/site/html' pattern='!-f' => matched –  Hugh Guiney Nov 10 '10 at 2:21
up vote 0 down vote accepted

Solved it using two modified versions of the regex from this page:

# $1 = directory, $2 = extension
# Matches:
# - /html
# - /web/portfolio/html

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/)?(html|rss|txt)$ $1index.php?transform=$2 [L]

# $1 = directory, $2 = "index.", $3 = extension
# Matches:
# - /index.html
# - /web/portfolio/index.html

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*/)?(?:$|(index\.)(?:([^.]*$)|$))$ $1$2php?transform=$3 [L]
share|improve this answer

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.