John John - 3 months ago 22
Apache Configuration Question

apache mod-rewrite to not include the top level domain

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png|swf|css|html|js|ico|pdf)$
RewriteCond %{REQUEST_URI} !^/app.php$
RewriteCond %{REQUEST_URI} !^/app.php/
RewriteRule ^(.*)$ app.php/$1?%{QUERY_STRING} [L]


OK so i have managed to created a nice little .htaccess rule set. It routes all traffic with the exception of images, html, js and css to a php file called app.php.

The only thing is, with this in place, it also routes the top level domain with or without a trailing slash to the app.php too.

How can I add the the toplevel domain to this ruleset?

Answer

If you want to avoid forwarding landing page then use:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !\.(gif|jpg|png|swf|css|html|js|ico|pdf)$
RewriteCond %{REQUEST_URI} !^/app\.php(/.*)?$ [NC]
RewriteRule ^(.+)$ app.php/$1 [L]

.+ will match anything but not the landing page.

In case you want to exclude all the top level domain URLS then use:

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|png|swf|css|html|js|ico|pdf)$
RewriteCond %{REQUEST_URI} !^/app\.php(/.*)?$ [NC]
RewriteRule ^(.*)$ app.php/$1 [L]