301 Redirect of old url with parameters to a path without parametes - apache

I need the following request :
http://somesite.com/home.php?action=page&page_id=9
to trigger a 301 redirect to :
http://somesite.com/a-new-page
Here's what I'm using but it isn't working.
RewriteCond %{QUERY_STRING} ^page_id=9$ [NC]
RewriteRule ^home\.php$ http://somesite.com/a-new-page? [L,R=301]
Any ideas?

If the QUERY_STRING will always be the same you need to match it like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^action=page&page_id=9$ [NC]
RewriteRule ^home.php$ http://somesite.com/a-new-page? [L,R=301]
Hope this helps.

Related

Change Query String using HTACCESS?

How do I change the query from
https://www.example.com/page.php?foo=bar
to
https://www.example.com/page.php?foo=something
just using htaccess ?
I had tried :
RewriteCond %{QUERY_STRING} foo\=bar$ [QSR]
RewriteRule page\.php\?foo\=bar$ page\.php\?foo\=baz [L,R=301]
not not working, testing it in the online htaccess tester -- https://htaccess.mwl.be/
You can use:
RewriteCond %{QUERY_STRING} foo=bar$
RewriteRule ^page\.php$ page.php?foo=baz [L,R=301]
Because the querystring is not part of the RewriteRule test.

Apache redirect & rewrite

This is my page
http://example.com/index.html .
I would like to make it work like this.
I would like to take the user to http://def.com when he requests
http://example.com/index.html
Even if he requests http://example.com/index.html?headers=0, he should be taken
to http://def.com
But if he requests the same URL with other query parameters, say for example http://example.com/index.html?footer=1, then the redirect or rewrite shouldn't happen. He should still be seeing the response from http://example.com/index.html?footer=1
I have already tried
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/index.html$ http://def.com
Change your htaccess file to this:
RewriteCond %{QUERY_STRING} ^(headers=0|)$ [NC]
RewriteRule ^index.html$ http://def.com [R=301,L]
Change your htaccess file to this:
RewriteCond %{QUERY_STRING} ^$ [OR]
RewriteCond %{QUERY_STRING} !headers
RewriteRule ^index.html$ http://def.com [R=301,L]

htaccess: rewrite and redirect 301

I rewrited, by .htaccess, a category of dynamic url generated by a query string in this mode:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
Now my rewrite works in right way and, for example, the following urls drive to the same page:
http://www.mysite.it/id1-01234-id2-56789
http://www.mysite.it/page.php?id1=01234&id2=56789
But now I want a redirect 301, from second type to first type, for all dynamic urls. For example:
from
http://www.mysite.it/page.php?id1=01234&id2=56789
to
http://www.mysite.it/id1-01234-id2-56789
The following way doesn't work:
RewriteEngine On
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id=$2 [L]
RewriteCond %{QUERY_STRING} (^|&)id1=$1($|&)
RewriteCond %{QUERY_STRING} (^|&)id2=$2($|&)
RewriteRule ^page\.php$ /id1-id2? [L,R=301]
Where is the error?
can you help me please?
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/page.php
RewriteCond %{THE_REQUEST} \?id1=(\w+)&id2=(\w+)\s
RewriteRule ^page.php /id1-%1-id2-%2? [NC,R=301,L]
RewriteRule ^id1-([^-]*)-id2-([^-]*)$ /page.php?id1=$1&id2=$2 [L]

htaccess redirect query string

How can I redirect a specific query to another URL in htaccess? The URL is:
http://miniwars.co.uk/player-search?location=&distance_max=&category%5B%5D=epic
The location, distance_max and category are always present, and I only want to redirect when location, distance_max are empty, and category is "epic".
I want to redirect to:
http://miniwars.co.uk/epic-players
The htaccess rule I'm trying is:
RewriteCond %{QUERY_STRING} ^(category=epic|location=|distance_max=)($|&)
RewriteRule ^player-search$ miniwars.co.uk/epic-players/ [L,R=301]
...But it's not doing anything. Can someone let me know where I've gone wrong?
Maybe you should try it like this.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(location=&distance_max=&category=epic)$ [NC]
RewriteRule ^player-search /epic-players/ [L,R=301]
I only want to redirect when location, distance_max are empty, and category is "epic".
You can use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)location=(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)distance_max=(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)category=epic(&|$) [NC]
RewriteRule ^player-search/?$ /epic-players/? [L,R=301,NC]
This will allow your query parameters in any order
? in the target URI will strip off previous query string

How to fix 301 redirect that is not working

I'm trying to create a redirect from https://www.compareking.no/penger/kredittkort to https://www.compareking.no/forbrukslaan but nothing is happening when I modify the htacess file.
This is the rewrite rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.compareking.no/penger/kredittkort$ [NC]
RewriteRule ^(.*)$ http:// www.compareking.no/forbrukslaan/$1 [L,R=301]
Thoughts?
HTTP_HOST only matches the HOST/domain part of the URL, that specified in RewriteCond and the URI prefix in RewriteRule would get you on the right track, as shown below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.compareking\.no$ [NC]
RewriteRule ^penger/kredittkort(.*)$ http://www.compareking.no/forbrukslaan$1 [L,R=301]