2

I have a Joomla site and I was trying to make URL redirection with query so when someone types

https://example.com/index.php/news/item/some-news 

they get redirected to

http://new.example.com/index.php/news/item/some-news

I was try this code but won't work

RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteCond %{QUERY_STRING} (news/item)
RewriteRule ^$ http://new.example.com/?%{QUERY_STRING} [R=301,L]
1
https://example.com/index.php/news/item/some-news 

There is no query string in either of the example URLs you've posted, so trying to match against the QUERY_STRING server variable will certainly fail. You are also trying to match against an empty URL-path (ie. ^$) - so this will fail from the get-go. The part of the URL you are trying to match is seemingly additional pathname information (aka "path info"). In other words, the additional URL-path after an existing filename.

Curiously, however, you are seemingly trying to redirect to a query string in your RewriteRule directive? You are also redirecting to HTTP (from HTTPS) - is that intentional?

On Apache you can either examine the PATH_INFO server variable or simply match as part of the normal URL-path using the RewriteRule pattern. So either of the following should work:

RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteCond %{PATH_INFO} ^/news/item/
RewriteRule ^index\.php http://new.example.com%{REQUEST_URI} [R=302,L]

OR,

RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteRule ^index\.php/news/item/ http://new.example.com%{REQUEST_URI} [R=302,L]

As per your directive, the condition of this redirect is simply that it matches /index.php/news/item/<anything>. And redirects to the same URL-path on the target domain.

This is a 302 (temporary) redirect. Only change this to a 301 (permanent) redirect after you have confirmed that it works OK. This is to avoid the browser caching erroneous redirects whilst testing.

You will need to clear your browser cache before testing.

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.