I have tried several examples of how to do this but nothing is working and I fear it is because the query string is part of the base URL.
Here is what I would like to do:
Redirect http://example.com/?from=TEST_1 -> http://example.com/foo
Redirect http://example.com/?from=TEST_2 -> http://example.com/bar
Here is what I have tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} from=TEST_1
RewriteRule (.*) /foo [L,R=301]
Unfortunately the above rule has no impact. If I change the rule to something static (see below) it works great:
Redirect 301 /foo /bar
Is there something special I need to do in order to check the query string value when it's part of the root domain request?
Try these 2 rules as your very first rules just below RewriteEngine line.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^from=TEST_1$
RewriteRule ^/?$ /foo? [L,R=301]
RewriteCond %{QUERY_STRING} ^from=TEST_2$
RewriteRule ^/?$ /bar? [L,R=301]
? at the end of target URI is used to discard previous query string from target.
^/?$ is the regex to match landing page only.
Related
I'm trying to do a .htaccess redirect with a parameter but it's not working. Seems like my regex are also wrong. :(
Original URL 1: http://www.example.com/?team=john-doe
Original URL 2: http://www.example.com/?team
Target URL: http://www.example.com/company/
I tried:
RewriteEngine on
RewriteCond %{QUERY_STRING} team=john-doe
RewriteRule /company/ [L,R=301]
Any help would be much appreciated.
Found a generator that works perfectly:
https://donatstudios.com/RewriteRule_Generator
# 301 --- http://www.example.com/?team=john-doe => http://www.example.com/company/
RewriteCond %{QUERY_STRING} (^|&)team\=john\-doe($|&)
RewriteRule ^$ /company/? [L,R=301]
Your RewriteRule is malformed, you are missing a pattern (first argument). Try something like:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^team(?:=john-doe)?
RewriteRule ^$ /company/? [R=301,L]
The RewriteRule pattern ^$ matches requests for the domain root. The ? on the end of the RewriteRule substitution strips the query string from the redirected URL (on Apache 2.4+ you can use the QSD flag instead).
The CondPattern ^team(?:=john-doe)? matches either "team=john-doe" (URL#1) or "team" (URL#2) at the start of the query string. The (?: part just makes it non-capturing.
You will need to clear your browser cache before testing.
How can I add s string to the end of a search result using .htaccess?
Original link: www.example.com/?s=search1 redirect to:
www.example.com/?s=search1&post_type=product
or
www.example.com/?s=search2 redirect to:
www.example.com/?s=search2&post_type=product
"search1" and "search2" are variable.
I need this to change a Wordpress search result to a Woocommerce search result.
What a tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+)
RewriteRule ^/?s=$ http://www.example.com/?s=%1&post_type=product [L,R=301]
Thank you in advance.
Your .htaccess seems fine, except for one thing: the RewriteRule Pattern will be matched against the part of the URL after the hostname and port, and before the query string, so this should do:
RewriteEngine On
RewriteCond %{QUERY_STRING} !post_type=product
RewriteCond %{QUERY_STRING} ^s=([^&]+)
RewriteRule ^$ http://www.example.com/?s=%1&post_type=product [L,R=301]
I need to redirect the request from
www.mysite.com/?querystring=data
to
www.mysite.com/dir/phpfile.php/?querystring=data
It means that it should be translated only the url with empty request_uri ( for example
www.mysite.com/css/style.css
should not be translated), and with not empty query string ( for example the main page
www.mysite.com/
should not be translated).
I wrote this code
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^/?(.*)$ www.mysite.com/$1 [QSA]
But it doesn't work. Any suggestion?
The rule that you have has the domain name in it without a protocol, it's going to look like a URI or file pathname to mod_rewrite. Additionally, you're rewriting it nothing, since $1 backreferences the grouping (.*) in your match, which must be nothing since your condition says the URI can only be /. You probably want something like:
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule ^/?$ /dir/phpfile.php/ [L,R]
The query string will automatically get appended to the end. Remove the R if you don't want to externally redirect the browser (thus changing the URL in the location bar), or replace it with R=301 if you want a permanent redirect.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.+)$ [NC]
RewriteRule ^$ /dir/phpfile.php/ [L,QSA,R=302]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
I want to create a condition that if page has parameter in URL like ?print=1 - redirect this page to itself without any querystring.
Example:
I want this page:
http://sos-med.com/en/specific_page.html?print=1&ok=1
tp redirect (301) to the same URL with no Query string:
http://sos-med.com/en/specific_page.html
My code is:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^*print=*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?
I have no test server, so can you tell me if my code is ok?
The code above is for every page on website. And before implementing that rule I would like to try the redirect for one specific page (see my example).
How to modify the code to work with "specific_page.html" only?
I want only .htaccess solution, not PHP code.
You're close, your %{QUERY_STRING} regex isn't right, and you're missing the 301 redirect flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}? [L,R=301]
Try that.
Thanks, and If I want to redirect single specific page: sos-med.com/en/aaa.html?print=1&ok=1 to sos-med.com/en/aaa.html ? –
Then you'd change what the rule matches against:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^en/aaa.html$ %{REQUEST_URI}? [L,R=301]
Try this one instead :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?print= [NC]
RewriteRule ^(.*)$ /$1? [L,R=301]
I've been trying to rewrite a URL such as
www.somesite.com/?x=372
into a url
www.somesite.com/
My current code does not seem to work
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule http://www.somesite.com/ [R=301,L]
I've looked up countless ways of trying to do it with htaccess and still no success.
If you simply want to redirect a client to remove the query string (everything after the ? in the URL), then you can try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule ^ http://www.somesite.com/? [R=301,L]
You've got most of it right, it seems, but your rule needs to have a match, and your target (the http://www.somesite.com/) needs a ? at the end so that any query string before the rewrite won't get appended.
In Apache 2.4 or newer you can use the QSD query string discard flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} x=(.*)
RewriteRule .* http://www.somesite.com/ [R=301,L,QSD]