简体   繁体   中英

How to rewrite URLs using htaccess only for images

This is the current URL of my images -

http://www.mysite.com/files/thumb/133/595

While inspecting it in firebug, this is the code under image tag -

<img src="http://www.mysite.com/files/thumb/133/595" alt="This is text">

Now i want the above URLs to be changed like this -

http://www.mysite.com/files/thumb/133/595/This-is-text.jpg

Image extension( .jpg in this case) added automatically on the type of image and alt text is added to the link with space being replaced from - .

How can i achieve this? via htaccess or some other method( project is in php ).

How to rewrite my htaccess file that this change will only take place for images only.(Newbie in re-writing).

You can't do this in htaccess, because the This is text" alternate text is never sent to the server and is only part of the HTML mark up. You need to change your site so that instead of:

<img src="http://www.mysite.com/files/thumb/133/595" alt="This is text">

At every place throughout your entire site, they'd look like this:

<img src="http://www.mysite.com/files/thumb/133/595/This-is-text.jpg" alt="This is text">

Not sure how your site is built, whether this is a one-line-change in some script or whether this involves a search and replace for every file. Once you have that, if you want to use mod_rewrite to rewrite these URL's back to the original , then you'd do something like this:

RewriteEngine On
RewriteRule ^/?files/thumb/([0-9]+)/([0-9]+)/.+\.jpg$ /files/thumb/$1/$2 [L]

Essentially, ditching anything after the 2 numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM