为图像重写URL


URL Rewrite for images

我的.htaccess文件

Options +FollowSymLinks
RewriteEngine on
RewriteRule     ^the-image-([A-Za-z0-9]+)-([A-Za-z0-9+]+)/?$  images/userfolder/$1/$2 [NC,L] #Loading Images

在我的PHP页面

<img src='./the-image-$username-$image_id' alt='User Image' />

我的目标是根据每个用户的用户名和图像ID加载图像。

用户名存储在$username下,图像ID存储在$image_id变量下。图像将位于images/userfolder/username/image.jpg 下方

但是这些图像并没有被加载。我的语法有什么错误?

注意:Image ID变量包含图像名称及其扩展名。示例:1.jpg

您可以试试这个:

RewriteRule  ^the-image-([^-]+)-([^-]+)/?$  images/userfolder/$1/$2 [NC,L]
^the-image-([A-Za-z0-9]+)-([A-Za-z0-9+]+)/?$

结尾处的正斜杠没有转义。尝试以这种方式更改正则表达式

^the-image-([A-Za-z0-9]+)-([A-Za-z0-9+]+)'/?$

或者,如果您从未期望$image_id 之后有任何正斜杠,则可以将其删除

^the-image-([A-Za-z0-9]+)-([A-Za-z0-9+]+)$