Forum Topic: Redirecting with ?=lang on the end

Forum: .htaccess Forum : Redirecting • Posted by supernaut • Updated:

I think this is a pretty simple question, but nonetheless. I am using DataTables on WordPress, each row in the table is pulled from custom fields (using ACF) of a separate sub-page. DataTables has the ability to filter the table based on an URL hash. As the sub-pages in themselves are never seen (just containers for the data that gets dumped in the table), I've set up a redirect for them to the DataTables page.

This is simple enough, and also separately replacing the url slug hyphens with spaces and sticking a # on the front so the table gets filtered properly.

The problem is I'm also using WPML, with the various non-default languages being declared with e.g. ?lang=en on the end.

My current redirect is:

RedirectMatch 301 ^/wordpress/([^/]+)/([^/]+)/$ http://kitaonline.com/wordpress/$1

(and the js which deals with the url slug is :

var search = '';
if ( window.location.hash !== '' ) {
	slug = window.location.hash.substring( 1 );
	search = slug.toString().replace(/-/g, ' ');
}

Which works fine for the default language. So, when there are seven languages, how can deal with the ?lang=nn ?

So if an URL is:

http://kitaonline.com/what-we-did/benedict-andrews-endstation-sehnsucht?lang=en

it gets redirected to:

http://kitaonline.com/what-we-did#benedict-andrews-endstation-sehnsucht?lang=en

Thanks for any help.

3 Replies to “Redirecting with ?=lang on the end”

Jeff Starr
Posted by Jeff Starr

If I understand correctly, the question is regarding the query string, and how to leave it intact through the redirect..? If that's the case, one way of doing it is with mod_rewrite, using something like this:

<IfModule mod_rewrite.c>
	RewriteBase /
	RewriteCond %{REQUEST_URI} ^/wordpress/([^/]+)/([^/]+)/$ [NC]
	RewriteCond %{QUERY_STRING} ^lang=(.*)$ [NC]
	RewriteRule .* /wordpress/%1?lang=%2 [L,R=301]
</IfModule>

That's a bit rough and may need some fine-tuning, but the concept shows how to leave the query string intact, by appending a question mark to the destination URL in the RewriteRule. The resulting query string may be modified with other variables, etc.

Hopefully that helps !

Posted by supernaut •

Hi Jeff,

thanks for your quick reply! I've been playing around with this a bit, and it seems there's a couple of things going on. First, site uses WPML, which has its own url rewrite (not sure if this is affecting the following), and second the default language doesn't have the query string, while all other languages do,

e.g. for the url in default language:

http://kitaonline.com/was-wir-gemacht-haben/benedict-andrews-endstation-sehnsucht

this redirects, and dumps the hash slug into the search field correctly (or it did for me just now, both with and without the query ?lang=de)

whereas:

http://kitaonline.com/what-we-did/benedict-andrews-endstation-sehnsucht

with or without the query first redirects to the hashed version, and then to the page url with query string:

http://kitaonline.com/what-we-did#benedict-andrews-endstation-sehnsucht
http://kitaonline.com/what-we-did?lang=en

The current redirect I'm using which is ok with the default language (with or without query string) is

RedirectMatch 301 ^/([^/]+)/([^/]+)$ http://kitaonline.com/$1#$2

and the original js now strips the query, so that also works ok for the search field, but I'm not sure what's going on with the secondary languages redirects which breaks it. Is this something in my redirect (and I'm happy to use mod_rewrite, just trying to debug where things work and don't)?

Quite lost here.

Thanks again!

Frances

Jeff Starr
Posted by Jeff Starr

Hi Frances, it sounds like some other .htaccess rules could be interfering with things.. I would try setting up a new installation of WordPress and do some tests from there. Start with just the default installation (no other htaccess rules or plugins), and then build up your site until the issue is discovered. My guess is that WPML is involved directly with the issue as you've described it.

Good luck!