14

Here's what I've got so far:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule .* index.php?url=$0 [L]

But when I go to soemthing like /a?b=c and then inspect the GET params, I only get a for url, and b is lost. How can I retain that?

mpen
  • 272,448
  • 266
  • 850
  • 1,236

2 Answers2

25

How about using the FallbackResource directive: "Define a default URL for requests that don't map to a file"

<Directory /web/example.com/htdocs/blog>
    FallbackResource /index.php
</Directory>

That way you don't have to call the modRewrite module on every request.

http://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource

NHenderson
  • 380
  • 3
  • 6
  • 1
    How does one get this to work if your index is not at the root directory? E.g. I want mysite.com/project/* to fallback to mysite.com/project/index.php. I could do FallbackResource /project/index.php, but it would be nice to somehow have a path relative to the .htaccess where it's defined (That way I can move project dir to projectX dir without modifying .htaccess) – Nathan Jan 17 '14 at 19:30
  • 2
    Isn't using the fallback actually slower than a direct redirect? Since the visitor now has to wait until the engine fails **all** matches before getting redirected to index.php. – Pacerier Oct 05 '14 at 23:45
18
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule .* index.php?url=$0 [QSA,L]

You need the QSA in your rewrite rule.

Dimitry
  • 6,545
  • 2
  • 20
  • 21
  • 1
    This also redirects my CSS and JS files...I would like those to keep working and pointing to where they should. – vsync Oct 12 '15 at 14:16
  • 2
    @vsync Then add a `RewriteCond` which does not match those files. For example: `RewriteCond %{REQUEST_URI} !^/js/` to not rewrite any files under the `js` directory. – Ken Wayne VanderLinde Dec 16 '16 at 05:26