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]
Related
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.
I'm trying to redirect any of these formats
mydomain.com/?main_page=products_new
mydomain.com/?main_page=products_new&page=2
mydomain.com/?main_page=products_new&disp_order=2
to this
mydomain.com/new-products
I have this working for only the first parameter. How can I ignore the remaining ones and match on anything that contains ?main_page-products_new ?
RewriteCond %{QUERY_STRING} ^main_page=products_new [NC]
RewriteRule ^(.*) /new-products? [L,R=301]
Apache supports RedirectMatch
RedirectMatch 301 ^main_page=products_new.*$ /new-products?$1
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]
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]
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]