简体   繁体   中英

htaccess url redirects with parameters not working

I did a search of previous questions about URL redirects with parameters, but none seem to speak to my particular problem. I'm not a programmer so I don't really know how to adapt other suggestions to my situation. Specific HTACCESS strings to try (and adapt for other URLs) would really help me.

I did an SEO restructure of my WP blog permalinks and I am finding that although the naked URLs are redirecting OK, URLs with parameters are not redirecting, they are going to a 404 error. I need URL parameters because my site is multilingual (Transposh plugin) so the "lang" parameter tells the site what language to translate the content to.

I think I may need to create a bunch of HTACCESS redirects that will redirect old URLs with a language parameter to the new permalinks for those URLs and pass the lang parameter through.

An example of this would be:

Source URL: /this-old-postname/?lang=(*)
Destination URL /blog/this-new-postname/?lang=$

There's no way to predict the URL pattern (although the parameter pattern is predictable) as each URL was tweaked for best SEO contribution.

I expect I'll need to write lots of these, each unique, so if you are able to provide an example can you please provide it for two redirects which would work for the following actual examples?

Source: http://www.travelnasia.com/thailand/bangkok/don-mueang-airport/?lang=zh
Destination: http://www.travelnasia.com/thailand/don-mueang-airport-bangkok/?lang=zh

Source: http://www.travelnasia.com/blog/map-attractions-bangkok-skytrain/?lang=zh
Destination: http://www.travelnasia.com/blog/bangkok-skytrain-bts-mrt-lines/?lang=zh

MOD_REWRITE is already enabled and standard redirects created in HTACCESS do work. I am pretty sure to achieve this I will need to use:

RewriteCond %{QUERY_STRING} ^lang=(.*)$

I understand this ensures the query string is read from the source URL.

I thought a redirect rule like this should work, but it doesn't:

RewriteRule ^test-redirect/$ http://test.travelnasia.com/destination/hanoi/hanoi-vietnam-destination-guide/$1 [L,QSA]

I've tried lots of other combinations but none of them seem to work.

Thanks in advance for any help offered.

Tony

If you don't mind including other query string parameters, [QSA] (query string append) is by far your simplest option.

Make it a 301 redirect with [R=301] if this is permanent change.

This should work for your purposes

RewriteEngine on # if not already enabled
RewriteRule ^thailand/bangkok/don-mueang-airport/?$ /thailand/don-mueang-airport-bangkok/ [QSA,R=301]
RewriteRule ^/blog/map-attractions-bangkok-skytrain/?$ /blog/bangkok-skytrain-bts-mrt-lines/ [QSA,R=301]
# ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM