Permanent Redirect of a variable URL - apache

I'm having troubles with my .htaccess permanent redirection code which is not working:
RedirectPermanent ^shop/?attro-attributes=([0-9]*) http://www.second-website.example
I would like to redirect URLs from first-website:
first-website.example/shop/?attro-attributes=01
first-website.example/shop/?attro-attributes=02
first-website.example/shop/?attro-attributes=...
first-website.example/shop/?attro-attributes=9999
to second-website URL: http://www.second-website.example/

RedirectPermanent and other redirect directives from mod_alias have no access to the query string. Your match pattern can't contain the ? from the URL or match on anything after it.
You'll instead have to use mod_rewrite which can access the query string via RewriteCond.
RewriteEngine on
RewriteCond %{QUERY_STRING} attro-attributes=[0-9]*
RewriteRule ^/?shop/$ https://www.second-website.example/ [L,R=301]

Related

Redirect / Rewrite with .htaccess of an url containing :

I'm trying to redirect from /index.php?option=com_k2&view=itemlist&task=category&id=44:pinco to google.it
Note the special char :
But it doesn't seem to work.
This is one of the tests I've tried. Where am I wrong?
redirect 301 /?option=com_k2&view=item&id=44:pinco http://www.google.it
You can not redirect Query String using Redirect directive. You need to check Query String using ℅{QUERY_STRING} variable.
RewriteEngine on
RewriteCond ℅{QUERY_STRING} ^option=com_k2&view=itemlist&task=category&id=44:pinco$
RewriteRule ^index\.php$ http://google.it? [L,R]

Simple 301 redirect in .htaccess with query string does not work with Redirect directive

I am trying to redirect a single URL in a .htaccess file with Redirect:
Redirect 301 /index2.php?option=com_rss&feed=RSS2.0&no_html=1 /something/somethingelse/
I have a bunch of other similar rules which work using directory structure URLs, but this one refuses to get processed.
Redirect 301 /old/url/ /new/url/
Do I have to do anything special?
Thanks!
With Redirect you can only test for URL paths, or more specifically, URL path prefixes but not for the URL query. But you can do so with mod_rewrite:
RewriteEngine on
RewriteCond %{QUERY_STRING} =option=com_rss&feed=RSS2.0&no_html=1
RewriteRule ^index2\.php$ /something/somethingelse/? [L,R=301]

How to do a htaccess rewrite with a question mark ? in the original url

I've got to redirect a URL that has a question mark in it, and I just can't figure out the proper .htaccess code to make it happen.
Example:
redirect 301 /home.php?cat=16 http://www.example.com/furniture.html
I believe I need to do a rewrite condition, but I'm no htaccess expert.
With the Redirect directive from mod_alias you can only examine the URI path and not the query (applies to all directives of mod_alias). If you want to examine the URI query you need to use mod_rewrite:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=16$
RewriteRule ^home\.php$ http://www.example.com/furniture.html? [L,R=301]
The empty query in the replacement will prevent that the original query is being appended to the new URI.
From the apache2 mod_rewrite docs:
What is matched?
The Pattern will initially be matched
against the part of the URL after the
hostname and port, and before the
query string. 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.
for more complex parameters (like site.com/index.php?blaa=1&id=33 )
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} blaa=1&id=33
RewriteRule ^(.*)$ http://www.site.com/yourpage.html? [R=301,L]
</IfModule>

Properly 301 redirect an URL with parameters to a different site

Right now I have an outdated URL that is getting 404'd:
http://mysite.com/category/?tag=blue-example
It needs to be redirected to:
http://subdomain.mysite.com/blue/
This doesn't work:
Redirect 301 /category/?tag=blue-example http://subdomain.mysite.com/blue/
How do I properly rewrite this URL?
The mod_alias does only chech the URI path and not the query (the part after the ?). But you can use mod_rewrite for this:
RewriteEngine on
RewriteCond %{QUERY_STRING} =tag=blue-example
RewriteRule ^category/$ http://subdomain.example.com/blue/? [L,R=301]

Apache redirect

I would like to redirect a URL using RedirectMatch within Apache eg,
/test/one/?? redirect to /test/two/??
where the ?? represents any string that follows
The redirect i'm using below does a straight redirect but doesnt match any string after...
RedirectMatch Permanent ^/test/one?$ /test/two/
Thanks alot
RewriteEngine ON
RewriteBase /
RewriteRule ^/test/one/(.+)$ /test/two/$1
if that does not work, change ^/test/one into ^test/one
make sure mod_rewrite is enabled
You can use mod_rewrite for this:
RewriteEngine On
RewriteBase /
RewriteRule ^/test/one/(.*) /test/two/$1 [L,R=301]
The R flag redirects the page rather than internally rewriting the URI. 301 is the HTTP status code for "Permanently Moved" - if you'd rather use another, you can change it to one of these.