在Apache HTTPD 2.2中,我希望对/index.foo或/foo请求(如果它们不是文件或目录)变成/index.php?transform=foo ,而不pipeURL有多less级别。
成功的标准是:
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 即,我不想重写相对于.htaccess目录; 我希望它们相对于请求中最顶层的目录。
# 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]
使用这个页面的正则expression式的两个修改版本解决它:
# $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]