ankush981
ankush981

Reputation: 5417

Simple URL rewriting fails

I have a very simple aim: Rewrite the URLs of my PHP app such that localhost/slim_demo/archive is interpreted as localhost/slim_demo/index.php/archive by the system but the user sees the former. EDIT: The system is behaving as if no rewriting is taking place. The latter version of the URL returns data, but the former throws a Not Found error.

I used the following .htaccess file, but it's not happening (by the way, as the second line says, uncommenting it rejects all requests, which shows that .htaccess is alive and kicking):

Options +FollowSymLinks -MultiViews -Indexes 
#deny from 127.0.0.1 #Uncomment to prove that .htacess is working
RewriteEngine On
RewriteRule ^slim_demo/(.*)$ slim_demo/index.php/$1 [NC,L]

And here's the relevant section from my apache2.conf:

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>
<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>
<Directory /media/common/htdocs>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

I also did a2enmod rewrite and service apache2 restart. Frustrated, I also added this to my sites-available and did a restart:

<Directory /media/common/htdocs/>
        Options +FollowSymLinks -Indexes
        AllowOverride All
</Directory>

Not sure what else I need to do!

Upvotes: 0

Views: 45

Answers (1)

Zimmi
Zimmi

Reputation: 1599

So if this .htaccess file is in the slim_demo directory, your RewriteRule never matches :

In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule

(The pattern is in your case the ^slim_demo/(.*)$ part).

This means that when you try to get the URL localhost/slim_demo/archive the slim_demo part is removed, and your rule never can match.

So you need:

RewriteRule ^(.*)$ index.php/$1

but this will bring you in an infinite loop and a 500 error. You must trigger this rule only if the REQUEST_URI does not have the index.php.

All together becomes:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^(?!/slim_demo/index\.php).*$
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA]

Upvotes: 2

Related Questions