4

I am attempting to rewrite URLs like the following:

domain.com/news/12/imgname.jpg

to

domain.com/image.php?img=12/imgname

I am using the following in my .htaccess file, but it does not seem to be working:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^news/([0-9]+\/.*)\.jpg$ image.php?img=$1 [L]

Can anyone help me see what I've done wrong?

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293

2 Answers2

0

You should not escape the forward slash. Try this:

RewriteRule ^news/([0-9]+/.*)\.jpg$ /image.php?img=$1 [L]
Olaf
  • 10,049
  • 8
  • 38
  • 54
  • This should not make any difference; mod_rewrite uses PCRE regexps, in which both `/` and `\/` match a forward slash. – Ilmari Karonen Nov 03 '12 at 16:35
  • Well it worked in http://htaccess.madewithlove.be/ - which does not mean it works in every environment. If you are right, however, the rewrite should work. – Olaf Nov 03 '12 at 16:36
  • Please try RewriteRule ^news/([0-9]+)/(.*)\.jpg$ /image.php?img=$1/$2 [L], which should definitely work. If not, you have another kind of error. – Olaf Nov 03 '12 at 16:39
  • I try it before and now too and not working. – user1796554 Nov 03 '12 at 17:02
0

Does your .htaccess file contain an appropriate RewriteBase directive? If not, add the following line before your rewrite rules:

RewriteBase /

You should also make sure you've enabled the mod_rewrite engine with:

RewriteEngine On
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^news/([0-9]+/.*)\.jpg$ /image.php?img=$1 [L]

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    Options -MultiViews

    Still not working
    – user1796554 Nov 03 '12 at 16:59
  • OK, so that's not it. Next questions: 1) does your rewrite rule work if you comment out the `RewriteCond`, and 2) if not, do _any_ rewrite rules actually work? – Ilmari Karonen Nov 03 '12 at 17:02
  • Pasting code into comments doesn't work very well. It's better to edit your question and include the code there. (Then again, in this, case, a simple "yes, I already use `RewriteBase /`" would've been enough.) – Ilmari Karonen Nov 03 '12 at 17:14
  • OK Thanks. It still not working and i don't understand Why. It must work. – user1796554 Nov 03 '12 at 17:25