1

I am using NextGEN Gallery Pro - Lightbox's Social Share feature on my WordPress site which actually create and share a temporarily created url (first-url) on social media. Somehow the redirect doesn't work and generates a blank page when I visit the first-url. I am thinking to add a manual htaccess rewrite rule or preferably WordPress function to make this redirect possible.

How can I add such rewrite rule in .htaccess or a WordPress hook to redirect first-url to second-url?

Redirect first-url:

example.com/nextgen-share/121212/8989/full?uri=/landscape-photography/

to

second-url:

example.com/landscape-photography/#gallery/121212/8989

I've tried several manual .htaccess rewrite rules but no joy. Any inputs will be much appreciated.

  • "Somehow the redirect doesn't work and generates a blank page" - What URL does it redirect to? Or does it not redirect at all? – MrWhite Nov 25 '19 at 15:21
  • No, it doesn't redirect at all and stays on first-url with a blank page. – Dipak G. Nov 25 '19 at 16:01
1

You could do something like the following in .htaccess before the existing WordPress directives.

RewriteCond %{QUERY_STRING} ^uri=(/[^&]+)
RewriteRule ^nextgen-share/(\d+/\d+)/full$ %1#gallery/$1 [QSD,NE,R,L]

The RewriteRule pattern only matches against the URL-path, so you need the preceding RewriteCond directive in order to match the query string portion of the URL.

If the /121212/8989/ part always consists of 6 digits and 4 digits then you can be more restrictive in the regex. ie. (\d{6}/\d{4}). Likewise, if the uri parameter value consists of a limited subset of characters - perhaps a single path segment - then again this can be made more restrictive.

%1 is a backreference to the captured subpattern in the preceding RewriteCond directive, ie. the value of the uri URL parameter.

$1 is a backreference to captured group in the RewriteRule pattern, eg. 121212/8989 in your example URL.

The NE (noescape) flag is required to prevent the # being URL encoded in the response (and being seen as part of the URL-path).

The QSD (Query String Discard) flag (Apache 2.4+) is required to remove the query string from the redirected URL, otherwise the query string from the requested URL is copied as-is onto the end of the substitution. If you are still on Apache 2.2 then you can append a ? to the end of the substitution instead (essentially appending an empty query string).

This is a temporary (302) redirect.

UPDATE: This will only work if 121212 but what if it's alpha-numeric (abc121212)

To allow a-z (lowercase) and digits then you would need to modify the RewriteRule pattern to ^nextgen-share/([0-9a-z]+/[0-9a-z]+)/full$. If you need to allow uppercase letters as well then change [0-9a-z] to [0-9a-zA-Z]. You could also use the \w "word characters" shorthand character class here instead, which is the same as [0-9a-zA-Z_] - note the additional _ (underscore).

So, this becomes:

RewriteCond %{QUERY_STRING} ^uri=(/[^&]+)
RewriteRule ^nextgen-share/(\w+/\w+)/full$ %1#gallery/$1 [QSD,NE,R,L]
  • I've updated my answer slightly... I've made the slash mandatory at the start of the uri parameter value, since this is required in order to construct a valid redirect. (Otherwise it will be reliant on the value of the RewriteBase directive.) – MrWhite Nov 25 '19 at 16:10
  • This will work if 121212 part is digits only but what if it's alpha-numeric (abc121212)? Does this also work if last part of the query string is /landscape-photography/sub--photography/ rather than just /landscape-photography/? I am playing with the rewrite rules you've provided already but your inputs will be much appreciated. – Dipak G. Nov 25 '19 at 16:21
  • You'll need to modify the regex to allow for letters - I've updated my answer. The current condition will already allow for /landscape-photography/sub--photography/ - providing the value starts with a slash. – MrWhite Nov 25 '19 at 16:27
  • It started to redirect but also adds query string portion at the end of second-url (new url). Means mywordpress_domain.com/landscape-photography/#gallery/121212/8989?uri=/landscape-photography/ – Dipak G. Nov 25 '19 at 16:34
  • I've added ? after $1 and it removed ?uri=/landscape-photography/ from the new url. Thanks. Here is the final rewrite rule: RewriteRule nextgen-share/([0-9a-zA-Z]+/[0-9a-zA-Z]+)/full$ %1#gallery/$1? [NE,R,L] – Dipak G. Nov 25 '19 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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