Question

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.

Was it helpful?

Solution

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top