WordPress : 301 redirection not working - php

I am using the following code to redirect a dynamic URL to a new dynamic URL, under the same domain in wordpress:
RewriteRule ^view-company/?id=(.+?)&name=(.+?)$ view-company/$1/$2/? [L,R=301,NC]
I want to redirect https://www.mydomains.com/view-company/id=1&name=abc to https://www.mydomains.com/view-company/1/abc
But this is not redirecting me to new url. Please help me.Thanks in advance

You can not match against query strings in RewriteRule's pattern, you need to match against %{QUERY_STRING} Variable using a RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([^&]+)&name=([^&]+)$
RewriteRule ^view-company/?$ https://www.mydomains.com/view-company/%1/%2? [L,R=301]
You can also manipulate querystrings using %{THE_REQUEST} Variable
RewriteEngine on
RewriteCond %{THE_REQUEST} /view-company/\?id=([^&]+)&name=([^&\s]+)
RewriteRule ^ https://www.mydomains.com/view-company/%1/%2? [L,R=301]

Related

htaccess redirect for redirecting all url with page to index.php

I need advise on to 301 permanent redirect using htaccess all pages having ?page=9 with index.php
Can anyone guide on how to achieve desired
For example -1
http://www.example.com/?page=9&option=com_news&view=list&Itemid=100&limitstart=840
to
http://www.example.com/index.php&option=com_news&view=list&Itemid=100&limitstart=840
or say for example 2
http://www.example.com/?page=17&option=com_news&view=list&Itemid=100
to
http://www.example.com/index.php&option=com_news&view=list&Itemid=100
Edit
Used
RewriteEngine on
RewriteCond %{THE_REQUEST} \?page=.+&(.+)\sHTTP [NC]
RewriteRule ^ /index.php&%1? [L,R=301]
But it dint worked as output is
http://www.example.com/index.php&limitstart=840
Assuming you actually want /index.php?option=... rather than /index.php&option=... - just matching on the querystring:
RewriteEngine On
RewriteCond %{QUERY_STRING} page=[^&]+&(.*) [NC]
RewriteRule .* /index.php?%1 [L,R,QSD]
However if you really do want /index.php&option=... replace the RewriteRule with:
RewriteRule .* /index.php&%1 [L,R,QSD]
Note: either way, this may not work as expected if page is not the first paramter
If your Apache version is older than 2.4 replace the RewriteRule with:
RewriteRule .* /index.php?%1? [L,R]
The [QSD] flag was only introduced with 2.4 - shouldn't be necessary anyway since the entire querystring is being matched.
You can use this :
RewriteEngine on
RewriteCond %{THE_REQUEST} \?page=.+&(.+)\sHTTP [NC]
RewriteRule ^ /index.php&%1? [L,R=301]

How do I redirect an asp url with query string to php url

There is an old link coming to my site that ends with sc_purchase_a.asp?discode=foo&ProID=bar. I want to redirect that to /courses/shop/4u.php?discode=foo. This is what I've done so far, but its not working.
RewriteRule ^sc_purchase_a.asp?discode=(\w+)&ProdID=(\w+)/?$ /courses/shop/4u.php?discode=$1 [QSA]
This doesnt seem to work.
Based on starkeen's answer, I came up with this, but it is not working and now gives me 500 errors.
RewriteCond %{THE_REQUEST} /sc_purchase_a\.asp\?discode=([^&]+)&ProID=.+ [NC]
RewriteRule ^ /courses/shop/4u.php?discode=%1 [L,R]
QueryString is not part of match in RewriteRules pattern. To manupulate query string ,you need to use a RewriteCond
RewriteEngine on
RewriteCond %{THE_REQUEST} /page\.asp\?code=([^&]+)&id=.+ [NC]
RewriteRule ^ /page.php?code=%1 [L,R]

redirect query string part if it's put after extension

I am using ultimate seo url in zen cart framework. I have a two type of URL structure which needs to be redirected.
1) http://www.naturaflowers.com/anemones/wedding-anemone-flowers.html?somestring
to
http://www.naturaflowers.com/anemones/wedding-anemone-flowers.html
2) (*** In this case when parameter only /?page=1) just like following
http://www.naturaflowers.com/wholesale-flowers.html?page=1 to
http://www.naturaflowers.com/wholesale-flowers.html
can't figure out. Any help will be appreciated. Please!!
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?somestring
RewriteRule ^(anemones/wedding-anemone-flowers.html)$ /$1? [L,R]
RewriteCond %{THE_REQUEST} \?page=1($|&)
RewriteRUle ^(wholesale-flowers.html)$ /$1? [L,R]

How to rewrite URL that contain parameters?

If user access this URL(http://domain.com/products/view-product.php?productID=21) in browser, it should show the page http://domain.com/new/view-product.php?pID=21
I have tried bellow code but it wont working, so please help me to resolve.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} products/view-product.php
RewriteRule ^products/view-product.php?productID=21$ new/view-product.php?pID=21 [R=301,L]
products/view-product.php are physically exists in the server, still i want to rewrite it to the new page that i have designed now.
You can't match against the query string (everything after the ?) in a rewrite rule. Only the URI is used to match against that regular expression. You need to use the condition:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} products/view-product.php\?productID=21($|\ |&)
RewriteRule ^products/view-product.php$ /new/view-product.php?pID=21 [R=301,L]
You can match query string using RewriteCond and %{QUERY_STRING} variable. Query string params didn't get to RewriteRule.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^productID=21$
RewriteRule ^products/view-product\.php$ /new/view-product.php?pID=21 [R=301,L]
Or if you want to redirect all productID's:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^productID=([0-9]+)$
RewriteRule ^products/view-product\.php$ /new/view-product.php?pID=%1 [R=301,L]
GET the value of productID and then redirect to the new URL.

Only one part of url needs to be 301 redirected

I need to redirect on a bunch or URL's in my website.
I have
http://www.myurl.lt/lt/imone/landstar-lt-lt/?cms_action=manage and I need to redirect it to http://www.myurl.lt/lt/imone/landstar-lt-lt/
I have about 700 of them, so is it possible to do a redirect with one redirect command?
This rules should remove the cms_action param, but preserve all other params
RewriteCond %{QUERY_STRING} ^(.+?&|)cms_action=[^&]*(?:&(.*)|)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=301,L]
Place this rule in /it/imone/.htaccess file:
RewriteEngine On
RewriteBase /it/imone/
RewriteCond %{THE_REQUEST} /([^?]*)\?cms_action=manage [NC]
RewriteRule ^ %1/? [R=301,L]
RewriteCond %{QUERY_STRING} !(^|&)cms_action=manage(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}?cms_action=manage [L,QSA]
Instead of using a Rewrite, which doesn't make any sense to me in this situation, why don't you just redirect the user directly from the manage action? This will definitely add to clarity. If you were using PHP for example:
// Inside manage action
header('Location: http://www.myurl.lt/lt/imone/landstar-lt-lt/');

Categories