Philip Philip - 5 months ago 8
PHP Question

Ignore a certain part of the URL via mod_rewrite

I have a Silex application and I get requests like this:
https://.../dev/foo/bar and want to rewrite them to https://.../index.php/foo/bar

Wheras "..." can be anything from "www.asd.com" to "localhost/~asd/folder".

My .htaccess file is located at the web root, https://.../

This is the normal mod_rewrite setup I'm using:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]


My naive try by setting the RewriteBase failed:

RewriteEngine On
RewriteBase /dev
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]


There I get the error

Forbidden

You don't have permission to access /dev/index.php on this server.


Also just rewriting failed:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dev/(.*) index.php [L]


Strangely, it doesn't just skip the dev part, but it tries to access /dev/foo/bar instead of the desired /foo/bar.

Any clue?

Answer

I found a solution for me by combining mod_alias and mod_rewrite like this, RewriteBase works now:

Alias /dev /app/web
<Directory /app/web>
    Options -MultiViews

    RewriteEngine On
    RewriteBase /dev
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
</Directory>