htaccess 301 permanent redirect using query string - .htaccess

I'm trying to do a 301 with a query string.
I want to redirect
www.mydomain.com/page1.php?id=12
To
www.mydomain.com/page2.php?id=12
I've already tried this in .htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page_id=([0-9]+)$
RewriteRule ^page2.php?page_id=%1 [R=301,L]

You may use this redirect rule:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=\d+$
RewriteRule ^page1\.php$ /page2.php [R=301,L,NC]
Query string will be automatically copied over to new URL.

Related

Rewrite rule 301 htaccess

Via .htaccess, I would like to create an automatic 301 from an old URL to a new url:
An example old url is: http://www.example.com/test.html?s=2&ss=3
I would like that to be automatically redirected to: http://www.example.com/test.html
If you want to match this specific URL and query parameters then you can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=2&ss=3^ [NC]
RewriteRule ^test\.html$ %{REQUEST_URI}? [L,R=302]
If you want to use this query string with any URI then use:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=2&ss=3^ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=302]
? after %{REQUEST_URI} is needed to strip off any query string.

Do 301 redirects work on urls that contain query strings?

I've searched for this but the answers I've found seem to be around rewriting URLs or for redirecting lots of pages in an entire section and look confusing (lots of regex).
We need to add some 301 redirects and everything is working fine except for 13 URLs that contain query strings.
Is there a trick to redirecting these kinds of urls?
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=39 /blogsearch/Coffee
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=26 /blogsearch/Food
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=27 /blogsearch/Wine
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=29 /blogsearch/Travel
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=37 /blogsearch/Hotel
Redirect 301 /blogsearch/?akID%5B30%5D%5BatSelectOptionID%5D%5B%5D=49 /blogsearch/Apartments
Any help would be much appreciated.
Cheers
Ben
Yes there's a trick. You can't use Redirect directive with query strings.
You could just do something like this with mod-rewrite. Just change the corresponding query string value for every individual link you have.
RewriteEngine On
RewriteCond %{QUERY_STRING} (.+)=39
RewriteRule ^blogsearch/?$ /blogsearch/Coffee? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=26
RewriteRule ^blogsearch/?$ /blogsearch/Food? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=27
RewriteRule ^blogsearch/?$ /blogsearch/Wine? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=29
RewriteRule ^blogsearch/?$ /blogsearch/Travel? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=37
RewriteRule ^blogsearch/?$ /blogsearch/Hotel? [L,NC,R=301]
RewriteCond %{QUERY_STRING} (.+)=49
RewriteRule ^blogsearch/?$ /blogsearch/Apartments? [L,NC,R=301]

url redirect exact match including params

i would like to make a 301 redirect and from an old website using the exact exact url with no extra parameters.
example:
/en-direct.php?page=7
to go to:
http://www.example.org/news/
and page:
/en-direct.php?page=8
to go to:
http://www.example.org/awesome-but-totally-different-page/
i used:
redirectMatch 301 ^/en-direct.php$ http://www.example.org/different-page/
redirectMatch 301 ^/en-direct.php?page=7$ http://www.example.org/news/
redirectMatch 301 ^/en-direct.php?page=8$ http://www.example.org/awesome-but-totally-different-page/
however: i get http://www.example.org/different-page/ every time with all the parameters from the redirect page (example - http://www.example.org/different-page?page=7 )
any help will be much appreciated.
In order to match against the query string, you need to use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=7($|&)
RewriteRule ^en-direct\.php$ http://www.example.org/news/ [L,R=301]
RewriteCond %{QUERY_STRING} ^page=8($|&)
RewriteRule ^en-direct\.php$ http://www.example.org/awesome-but-totally-different-page/ [L,R=301]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^en-direct\.php$ http://www.example.org/different-page/ [L,R=301]

301 Redirect Get Parameter URL with htaccess

I need to 301 redirect an old url that contained a get parameter in the url.
I need to 301 the URL:
http://www.website.com/choose?cat=womens
to this URL:
http://www.website.com/womens
I have searched and tried without it working:
RewriteCond %{QUERY_STRING} cat=womens
RewriteRule ^choose\.php$ /womens [L,R=301]
Where am I going wrong?
You're almost correct, just 2 issues:
.php wasn't there in your original URI after choose as per the question
You need to add ? in target to strip original query string
You can use:
RewriteCond %{QUERY_STRING} (?:^|&)cat=([^&]+) [NC]
RewriteRule ^choose(?:\.php)?$ /%1? [L,R=301,NC]
Try these:
RewriteCond %{QUERY_STRING} ^cat=womens$
RewriteRule ^choose$ http://www.website.com/womens? [R=301,L]

301 redirect rule through htaccess issue

I want to 301 redirect from
domain.com/?test-article.php
to
domain.com/full-article/test-article.php
Here is my code what i am trying to use and its not working.
redirect 301 /?test-article.php http://domain.com/full-article/test-article.php
or
rewriteRule /?test-article.php http://domain.com/full-article/test-article.php [R=301,L]
if i use without '?' then it re-directs from
/test-article.php
to
http://domain.com/full-article/test-article.php
So something has to do with '?'
You can't match against the query string using a pattern of a Redirect or RewriteRule. You need to use the %{QUERY_STRING} variable and mod_rewrite:
RewriteCond %{QUERY_STRING} ^test-article$
RewriteRule ^/?$ http://domain.com/full-article/test-article.php [R=301,L]

Resources