1

I have the following URL:

http://example.com/gallery/thumb.php?h=400&w=300&a=c&src=Img_0.jpg

Which I'm trying to use mod_rewrite to make "pretty".

The desired URL is:

http://example.com/h/400/w/300/a/c/src/Img_0.jpg

And my mod_rewrite is:

RewriteRule ^h/(*)/w/(*)/a/(*)/src/(*)$ /gallery/thumb.php?h=$1&w=$2&a=$3&src=$4 [L]

But I'm getting a 500 Internal Server Error error which tells me I've written this rule wrong.

What did I write wrong about it?

EDIT: Not a duplicate. My question is pertaining to a particular code that I tried to write myself and did not manage to write a working code.

Jesse Elser
  • 113
  • 5
  • 2
    Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](http://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Jenny D Oct 10 '16 at 04:54
  • The question linked contains information about how to turn on rewrite debugging. Use that. – Jenny D Oct 10 '16 at 04:54
  • 3
    Maybe just terminology, but you don't `use mod_rewrite to make "pretty"` - you make the URLs "pretty" in your application _first_. You then use mod_rewrite to turn (ie. _internally rewrite_) the "pretty" URL back into the (real) "ugly" URL (which is what you are doing above). – MrWhite Oct 10 '16 at 07:41

1 Answers1

3

This is not valid in your regular expression: (*).

* denotes a repetition of the previous character. Since you don't have any character in the group there is nothing to repeat.

If you change (*) to (.*) the expression becomes valid. . denotes "every character", so you might want to restrict that a little further.

An expression for your example could be:

RewriteRule ^h/(\d+)/w/(\d+)/a/([a-z]+)/src/(.+)$ /gallery/thumb.php?h=$1&w=$2&a=$3&src=$4 [L]

Where \d denotes a digit and [a-z] any character in this range. I also changed * to +, which matches on "1 or more characters", instead of "0 or more", which would be the *.

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89