Marek J.
Marek J.

Reputation: 317

RewriteRule acting strange

I want URL like this: localhost/dir/images/pic1.jpg

to be rewriten to: localhost/dir/subdir/index.php?folder=images&picture=pic1.jpg

So i put really simple .htaccess file in localhost/dir/:

RewriteEngine On
RewriteRule ^(.*)/(.*)$ subdir/index.php?folder=$1&picture=$2 [L]

and expect to get folder='images' and picture='pic1.jpg' in localhost/dir/subdir/index.php but instead I have folder='subdir' and picture='index.php'

 

Strange thing is that when i modify .htaccess file to call index.php from the same directory (not from 'subdir') it works well:

RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?folder=$1&picture=$2 [L]

I get folder='images' and picture='pic1.jpg' in localhost/dir/index.php script

Upvotes: 1

Views: 28

Answers (1)

anubhava
anubhava

Reputation: 784918

That is happening because your rewrite rule is looping and matching target string subdir/index.php with the pattern .*/.*.

Use this condition to stop the loop:

RewriteEngine On

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*)$ subdir/index.php?folder=$1&picture=$2 [L]

Upvotes: 1

Related Questions