I am using standard WordPress gallery, but I am not happy with the url to image (link to attachment page).

In my current situation gallery url is:


and image url is


What I am trying to achieve is that gallery url will remain the same, but image (attachment) url will be:


I can customize my gallery code to add the gallery slug to attachment url (probably by creating custom shortcode which will get content of gallery shortcode and just replace urls), but I am not able to make .htaccess file to work (if is there better way then using .htaccess file, I don't mind that).

My current .htaccess code is

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /main/dmm/wp01/

# beginning of my code
RewriteRule ^selected-work/(.*)/ $1/
# end of my code

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /main/dmm/wp01/index.php [L]
# END WordPress

# Caching disabled    
<filesMatch "\.(html|htm|js|css)$">
  FileETag None
  <ifModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
# END OF Caching disabled
  • I have just realized that even if this would work, I should change it because it will probably be removed during WordPress update. Commented Dec 10, 2016 at 14:14
  • URL rewriting is handled by WordPress, it takes a URL and converts it into query parameters that are used to fetch things from the DB. A HTAccess rewrite isn't going to help much as WP is unaware of what you've done. Instead you're actually asking a rewrite rules question, and htaccess is a distraction. Your solution was always going to be WP based though as your galleries won't generate permalinks with the /gallery/ prefix that you want
    – Tom J Nowell
    Commented Dec 10, 2016 at 15:31
  • Also, if you can remove some ambiguity, you say the gallery URL, does that mean you want all attachments to literally take the form /gallery/image1, or is the word gallery representative of something, e.g /firstgallery/image1 /summer/image1, /winter/image1, or is there a page called gallery?
    – Tom J Nowell
    Commented Dec 10, 2016 at 15:34
  • The second is my case /firstgallery/image1 /secondgallery/imagew2 and so on. I am pretty close to my solution using WP Rewrite API. I am going to post my answer as soon as I have it fully working. Commented Dec 10, 2016 at 16:00

1 Answer 1


So I was able to make it work (without .htaccess as Tom sugested) using WordPress Rewrite API. Here is my working code (from functions.php):

function add_rewrite_rules( $wp_rewrite )
    $new_rules = array
        '(the-gallery-slug)\/(.?.+?)(?:/([0-9]+))?/?$' => 'index.php?pagename=$matches[2]'

    // Always add your rules to the top, to make sure your rules have priority
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;

add_action('generate_rewrite_rules', 'add_rewrite_rules');

And here are links that helped me a lot:

"Wordpress - wp_rewrite rules" post on stackoverflow

Debug WordPress 404 issues (permalinks, rewrite rules, etc.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.