Can't seem to get this working, I want to rewrite some old query-driven URLs to a new format:
URL Before: http://www.example.com/blog/?lang=fr
RewriteCond %{QUERY_STRING} ^[^&](\w+)=(\w+)$ [NC]
RewriteRule ^ %2%{REQUEST_URI} [QSD,R=301,L]
URL After: http://www.example.com/fr/blog/?lang=fr
Close, but no cigar! I've tried a few SO solutions (i.e. Rewrite urls in htaccess file - remove query string) but I think that I'm missing something.
QSD only works since version 2.4 of Apache. If you are using an older version, it works with :
RewriteCond %{QUERY_STRING} ^[^&](?:\w+)=(\w+)$ [NC]
RewriteRule ^ %1%{REQUEST_URI}? [R=301,L]
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.
I'm using Helicon Ape on a Windows Server to create htaccess files.
Originally, part of a larger set of conditions, I had this condition set to return 403 if the url contained (). However it is causing false positives in case of mailchimp tracking codes that end up getting wrapped in ()
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]
For example in the below URL
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
As an alternative, I was attempting to remove the parenthesis and redirect to a "cleaned version".
I tried a few different things that I found in SO but none worked.
So far the closest thing that I could get to working is this:
RewriteCond %{REQUEST_URI} [\(\)]+ [OR]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
The problem with the above code is that works if the () were in the URL but not the query string. It doesn't redirect and clean the querystring.
So this would work:
http://domain.com/page/11/pag(e-name)
but this wouldn't:
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
Your assistance is appreciated
Thank You.
You can use the following rule :
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=t\(Newsletter_Tracking\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
If the querystring is dynamic, try:
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=.+\(.+\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
Using #starkeen 's example, I was able to create a working solution.
This code handles the Query String separate from the URL. It cleans the URL but removes the query string.
RewriteCond %{REQUEST_URI} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
I know with a simple redirect one (Sub-)Domain or Folder to another i can get rid of a string like this
RewriteEngine On
RewriteRule (.*) http://www.domain.tld/? [R=301,L]
I know how to get rid of it when it is a simple file too.
But when it comes to the Rootdomain itself (http://domain.tld/?Stringwhatsoever), i am at a loss here. My last try used a modified version of a redirect I used to redirect files and folders around and that worked pretty nicely and also removed the query, but it ended up in a redirection error.
RewriteRule ^ http://domain.tld/? [L,NC,R=301]
So i have no clue how to get rid of Query Strings at urls without breaking it.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} /\?([^\s]+) [NC]
RewriteRule ^$ http://domain.com/? [NC,R,L]
Or
RewriteEngine On
RewriteCond %{QUERY_STRING} (.+) [NC]
RewriteRule ^$ http://domain.com/? [NC,R,L]
Reference :
-https://wiki.apache.org/httpd/RewriteQueryString
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]
I'm trying to do a very simple redirect. We're moving from a standalone Wordpress install to a Multisite Wordpress install, and I'm trying to get the redirect working for a page with query strings. Here's the code I have so far:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^advise/index_pt(.*)$ http://domain.com/advise/pennies-to-millions/$1 [NE,L,R=301]
Basically, this is what I want: (with or without www)
*.domain.com/advise/index_pt.php?(parameters) => *.domain.com/advise/pennies-to-millions/?(parameters)
Any help is appreciated!
Query strings aren't part of the URI used to match in a RewriteRule directive. So you can't use a regular expression to match against them inside a RewriteRule. If you needed to match against them, you can use a RewriteCond %{QUERY_STRING} <regex> before the rule. However, query strings are automatically appended anyways if you don't create any new ones by using a ? in the rule's target. You can remove all the backreferences in the rule and add parens around the www to make them optional.
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^advise/index_pt.php$ http://domain.com/advise/pennies-to-millions/ [NE,L,R=301]