htaccess redirects are "ignored" - .htaccess

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&...

Related

htaccess - Redirect unless any query

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.

RewriteCond %{QUERY_STRING} for all

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+

301 Redirect On Root Query Params

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.

Remove variable from base URL with htaccess

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]

htaccess - rewrite rule to get end of url

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]

Resources