We used to have a url pattern that was something like www.website.com/wmspage.cfm?parm1=1000 with 1000 being the page ID.
I did a few 301 rewrites like this:
RewriteCond %{QUERY_STRING} parm1=513
RewriteRule ^(.*)$ http://www.website.com/new-url/? [R=301,L]
BUT now I want to actually take all of these old parm1=id strings to 301 them to our homepage rather than doing these redirects on an individual basis. How do I make this a universal redirect?
Is there way to capture all urls with parm1=whatever?
To catch all IDs for parm1, just use the \d+ expression to catch all digits in your condition:
RewriteCond %{QUERY_STRING} parm1=\d+
Related
I am redirecting most pages, including the index, from firstdomain.com to seconddomain.com.
To redirect the pages, I use:
Redirect 301 /faq http://www.seconddomain.com/faq
Redirect 301 /contact http://www.seconddomain.com/contact
To redirect the index, I use:
RewriteRule ^$ http://www.seconddomain.com/ [R=301,L]
Now, the problem is that I want http://www.firstdomain.com/?query=9328 to remain. With the above code it redirects to http://www.seconddomain.com/?query=9328
My question is: How do I redirect the index unless it contains a query?
Prefix the RewriteRule with a RewriteCondusing a Negative lookahead on the query string, for example
RewriteCond %{QUERY_STRING} (?!query=9328)
RewriteRule ^$ http://www.seconddomain.com/ [R=301,L]
This will skip the redirect if the query string contains the pattern. If you have valid query parameters such as otherquery, etc. then you might want to anchor the pattern with \b bookends.
This works:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ http://www.seconddomain.com/ [R=301,L]
I have no idea if it's the correct or best way, but it does redirect the index unless it's followed by a query string.
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.
I'm using magento and every url is acessible in two ways for example:
http://www.mysite.com/product-item, and
www.mysite.com/product-item?___store=default
I tried a regular 301 redirect in .htaccess but it won't redirect. Does anyone have the code that would automatically redirect all url's with the suffix ?___store=default back to the clean url's thus avoiding duplicate content issues?
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?___store=default
RewriteRule ^(.*)$ /$1? [L,R=301]
or
RewriteEngine On
RewriteCond %{QUERY_STRING} ___store=default
RewriteRule ^(.*)$ /$1? [L,R=301]
depending on how the query string is generated. The reason why a Redirect 301 probably didn't work is that you can't match against query strings using that statement.
Have looked at all the other posts regarding this, but can't seem to get it to work. There are a number of unrelated reasons why I can't change the Joomla config, items, etc., but in any event, it seems that these should work regardless of that.
In short, I want any link with Itemid=30 in it to redirect to the one given. What am I doing wrong? The first 3 are what I've tried, that last line is one that is working.
RedirectMatch 301 ^Itemid=30$ http://inside.beta.oursite.com/index.php?option=com_cware&view=courses&Itemid=125
redirect 301 index.php?option=com_cware&view=courses&Itemid=30 http://inside.beta.oursite.com/index.php?option=com_cware&view=courses&Itemid=125
RewriteEngine On
RewriteCond %{QUERY_STRING} Itemid=30
RewriteRule ^Itemid=30/$ index.php?option=com_content&view=article&id=106&Itemid=121 [R=301]
# If query string contains "username=admin"
RewriteCond %{QUERY_STRING} username=admin
# Block it without mercy
RewriteRule .* - [F]
Redirect considers only URL paths, which excludes the query string. If you want to take the query string into account, you must employ mod_rewrite.
Same applies to RewriteRule
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
Therefore, if you want to match some query string and redirect all URLs to some other URL, use a RewriteCond in combination with RewriteRule
RewriteEngine On
RewriteCond %{QUERY_STRING} Itemid=30
RewriteRule .* index.php?option=com_content&view=article&id=106&Itemid=121 [R=301]
This redirects any URL .* with the query string containing Itemid=30 to index.php?option=com_content&...
I have a url which ends with a certain variable string, and was erroneously generated and indexed unfortunately.
Example:
http://domain.com/anything-in-between/?var=xyz-abc-abc-abc
How can I redirect to main site (kill it), by detecting 'abc-abc-abc' using htaccess?
Why wouldn't this work and what would be the best solution:
RewriteCond %{REQUEST_URI} abc-abc-abc
RewriteRule .* index.php
You want to use the query string as claesv suggests but you need to then kill the query string
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} \bvar=.*?abc-abc-abc$
RewriteRule ^ index.php? [L]
This will do it silently (i.e. in the server as an internal redirect and not involving the browser). You can't use 301s reliably to trim query strings.
Something along the lines of:
RewriteCond %{QUERY_STRING} ^var=.*abc-abc-abc$
RewriteRule ^.*$ http://domain.com/ [R=301,L]