Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Rewrite/redirect url WordPress

  • SOLVED

Hi - not sure if this requires htaccess or a function.

I want to rewrite an image url to a normal page.

Eg,

http://www.example.com/wp-content/uploads/2015/08/demo-image.jpg

to

http://www.example.com/new-folder/demo-image/

or

http://www.example.com/new-folder/second-folder/demo-image/


a) has to work for all folders (dates) in /uploads

b) the image filename is used without the extension

The pagename will always be the same as the image filename - without the extension.

I'd also like a version that works for redirecting to a new domain

http://www.example.com/wp-content/uploads/2015/08/demo-image.jpg

to

http://www.newdomain.com/new-folder/demo-image/

Answers (1)

2015-08-03

Dbranes answers:

I haven't tested it, but you could try something like:

RewriteEngine on
RewriteRule ^wp-content/uploads/.*?([^/.]+).(jpg|gif|png)$ http://www.newdomain.com/new-folder/$1/ [QSA,NC,R=301,L]


for the redirect from

http://www.example.com/wp-content/uploads/2015/08/demo-image.jpg

to

http://www.newdomain.com/new-folder/demo-image/


Austin comments:

I put in in the main .htaccess - unfortunately it didn't redirect the image though.


Dbranes comments:

This worked for me:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-content/uploads/.*?([^/.]+).(jpg|gif|png)$ http://www.newdomain.com/new-folder/$1/ [QSA,NC,R=301,L]
</IfModule>


and also this one:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/wp-content/uploads/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ([^/]*).(jpg|gif|png)$ http://www.newdomain.com/new-folder/$1/ [QSA,NC,R=301,L]
</IfModule>


Dbranes comments:

This worked for me:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-content/uploads/.*?([^/.]+).(jpg|gif|png)$ http://www.newdomain.com/new-folder/$1/ [QSA,NC,R=301,L]
</IfModule>


and also this one:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/wp-content/uploads/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ([^/]*).(jpg|gif|png)$ http://www.newdomain.com/new-folder/$1/ [QSA,NC,R=301,L]
</IfModule>


Austin comments:

Yup, that works - thanks.