htaccess redirect only query without string - .htaccess

I need to redirect following URL as follows:
https://www.example.com/news/amp/?s
to
https://www.example.com/amp?s
2nd URL has no "/" before ?s
I tried using following code, but failed.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^news/amp/?s
RewriteRule ^(.*)$ http://www.example.com/amp?s [R=301,L]
But its not working, can any one suggest correction for this code?

Try with below we are using %{THE_REQUEST} for matching instead of %{QUERY_STRING} which match only query string.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^/news/amp/?s
RewriteRule ^(.*)$ http://www.example.com/amp?s [R=301,L]

Related

How redirect query search result from domain which contains a folder?

I have this code in my .htacces file:
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ job\?search_keywords=%1 [NC,R,L]
And it works well for: example.com/?s=blabla
Now I would like to add that if the url contains /en/:
https://example.com/en/?s=blabla
change for https://example.com/en/work/?search_keywords=blabla
Somone could help me with it? Because my trying gives me ERROR 500.
You just need another rule to handle new URI pattern:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^$ /job?search_keywords=%1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^en/?$ /en/work/?search_keywords=%1 [R=301,L]
Note that \\? in your RewriteCond is redundant as QUERY_STRING variable doesn't contain ?

use ? in first of url

I want use below link and load from category.php but it does not work
http://test.com/?search=x
.htaccess:
RewriteRule ^search$ category.php?search=$1 [QSA,NC]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test\.com$
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ http://category.php?search=%1 [R=302,L]
The reason you can't get away with a single RewriteRule is that query parameters cannot be directly rewritten. Instead, the above code captures the query string using RewriteCond %{QUERY_STRING}, which is then made available as %1 in the RewriteRule which follows. The RewriteCond %{REQUEST_URI} ensures that the code will only evaluate for the test.com domain.
Try this,
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ category.php?search=%1 [QSA,NC,L]
You can use this simple rule:
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^/?$ category.php [L]
Pattern ^/?$ ensures that we match only landing page, nothing else.
QUERY_STRING is automatically appended to target URI so there is no need to capture and append in target URI.

Htaccess appending to query string with QSA without redirect

I am trying to add English version to my website in subdomain level as following:
en.mywebsite.com/business-name : This will show the English version of a sub-page.
Right now I have actually achieved it somehow by appending the subdomain to the query string with the following code:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com [NC]
RewriteCond %{HTTP_HOST} !^en\.mywebsite\.com [NC]
RewriteRule (.*) http://www.mywebsite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^en\.mywebsite\.com
RewriteRule ^(.*)$ http://www.mywebsite.com/$1?language=en [QSA]
The problem is, it is doing it by redirecting the user to www.mywebsite.com/business-name?language=en, but I want to avoid redirection, and pass the "newly generated" query string virtually instead.
Can it be achieved, or does QSA always redirect?
Thanks in advance.
Try changing your last rule from:
RewriteCond %{HTTP_HOST} ^en\.mywebsite\.com
RewriteRule ^(.*)$ http://www.mywebsite.com/$1?language=en [QSA]
to
RewriteCond %{HTTP_HOST} ^en\.mywebsite\.com
RewriteCond %{QUERY_STRING} !language=
RewriteRule ^(.*)$ /$1?language=en [L,QSA]

Redirecting a URL with query string

I've got a URL with a query string that I am trying to redirect and I just can't get it to work. The original URL not only has a query string, it's a Joomla SEF URL and contains a ? within the URL as well. It's a mess. This is the original URL:
http://www.domain.com/menu-item/item/1234-article-title-here?.html&utm_source=XYZ&utm_medium=ABC&utm_campaign=the+campaign+name
I've tried several different rewrite conditions and rules and none I have tried are working. I am also putting the statements in .htaccess in the root since there are no actual sub directories. The query string does not need to transfer.
I tried this and it didn't work:
RewriteCond %{REQUEST_URI} ^/menu-item/item/1234-article-title-here\?\.html$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
I also tried
RewriteCond %{REQUEST_URI} ^/menu-item/item/1234-article-title-here$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
And this
RewriteCond %{REQUEST_URI} ^1234-article-title-here\?\.html$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
and this
RewriteCond %{REQUEST_URI} ^1234-article-title-here$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
As well as the answer from #jon lin. I ended up finding a redirect that worked, posted below.
This is what finally ended up working:
RewriteCond %{REQUEST_URI} ^/menu-item/item/1234-article-title-here$
RewriteRule ^(.*)$ http://www.domain.com/new-page.html? [R=302,L]
Have you tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^\.html&utm_source=XYZ&utm_medium=ABC&utm_campaign=the+campaign+name$
RewriteRule ^menu-item/item/1234-article-title-here$ /page-you-want-to-redirect-to? [L,R=301]
The ? at the end of the target removes the query string so that it doesn't get included in the redirect.

.htaccess redirect one domain to another incliuding specific query string

basally I need to redirect
http://www.old-domain.com/news.php?NewsID=30888
to
http://www.new-domain.com/news.php?NewsID=30888
using htaccess however only if the query string is as above. Any other query strings I need to continue to go tohttp://www.old-domain.com/news.php?NewsID=whatever_querystring.
So I need to match the 30888 and then redirect to a whole new site including the news.php?NewsID=30888.
Thanks in advance
Put this code in your .htaccess and try;
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^NewsID=30888 [NC]
RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$ [NC]
RewriteRule ^ http://www.new-domain.com%{REQUEST_URI}?%{QUERY_STRING} [R=301,L]
You have to test your Query string and the host name:
RewriteCond %{QUERY_STRING} NewsID=30888 [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^/(.*) http://www.new-domain.com/$1 [L,R]

Resources