htaccess redirect rule for multiple URL params - apache

I need to get a redirect using htaccess
From: http://example.com/go/zaym?source=`PARAM1`&keyword=`PARAM2` but if there are no URL params I need also to get redirect with empty params
To: another domain http://example2.com/sub_id=PARAM`&sub_id2=PARAM2
So I need to move value source to sub_id and value keyword to sub_id2
I'm trying so, but it's not working:
RewriteCond %{QUERY_STRING} ^source=([^&]+)&keyword=([^&]+)$
RewriteRule ^go/zaym http://example2.com/sub_id=%1&sub_id2=%2 [R,L]

EDIT(More generic way): With more generic way as per OP's comment one could try following, where no hard coding of keyword or source strings, then try following.
With only query string condition check:
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^[\w-]+=(.*)(?!=&)$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1 [NE,L]
RewriteCond %{QUERY_STRING} ^[\w-]+=([^&]*)&[\w-]+=(.*)$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1&sub_id2=%2 [NE,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteRule ^ http://example2.com%{REQUEST_URI} [L]
With URI + query string condition:
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^[\w-]+=(.*)(?!=&)$ [NC]
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1 [NE,L]
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteCond %{QUERY_STRING} ^[\w-]+=([^&]*)&[\w-]+=(.*)$ [NC]
RewriteRule ^(.*)$ http://example2.com%{REQUEST_URI}?sub_id=%1&sub_id2=%2 [NE,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} ^/go/zaym/?$ [NC]
RewriteRule ^ http://example2.com%{REQUEST_URI} [L]

Related

Match complete path of a URL to redirect

I want to redirect a path and all subfolders under this path.
For Example ich want to match the URLs
https://www.joka.de/produktfinder
https://www.joka.de/produktfinder/foo
https://www.joka.de/produktfinder/foo/bar/test.html
The following code works only for the /productfinder but not for productfinder/test
RewriteCond %{HTTP_HOST} ^.*joka.*$ [NC]
RewriteCond %{REQUEST_URI} ^/produktfinder* [NC]
RewriteRule ^(.*)$ https://www.jordan-kassel.de/ausbildung.html [L,R=301]
I think this could be the solution
RewriteCond %{HTTP_HOST} ^.*joka.*$ [NC]
RewriteCond %{REQUEST_URI} ^/produktfinder(.*) [NC]
RewriteRule ^(.*)$ https://www.jordan-kassel.de/ausbildung.html [L,R=301]

.htaccess external & internal rewrite conflict

I'm trying to rewrite a query string to a path, like so:
http://example.com/?p=page1
to
http://example.com/page/page1
The internal redirect works and I can view the page at the second URL but as soon as I try to redirect the first URL to the second, I get a 'Too many redirects' error.
.htaccess:
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
The first two lines are working by themeselves. The addition of the last two lines causes the error.
Your rule is redirecting the uri back it itself that is why you got the redirect error.
You can use %{THE_REQUEST} or %{ENV_REDIRECT_STATUS} variables to avoid Too many redirect error .
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
or
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /\?p=.+ [NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE]

Exclude one url from rewrite condition

I have following mod_rewrite rule to redirect from site.com to www.site.com:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L] .
I need to exclude from this rule urls starting with /yandex_market
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{REQUEST_URI} !^/yandex_market.*$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L] .
But rule still works on urls starting with /yandex_market How to fix it?
Problem is that your 2nd rule rewrites /yandex_market/foo URI to /index.php?module=YandexPurchaseView&type=foo and thus making RewriteCond %{REQUEST_URI} !^/yandex_market.*$ [NC] condition succeed. You will need to use %{THE_REQUEST} variable for your condition which doesn't change with application of rewrite rules.
Keep your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{THE_REQUEST} !/yandex_market [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NE,L]
RewriteRule ^yandex_market/(.+)$ index.php?module=YandexPurchaseView&type=$1 [L,NC,QSA]

htaccess redirect if certain query pattern found

i would like to redirect from
www.site1.info?index.php?id=243&L=&tx_kesearch_pi1%5Bsword%5D=YOU
www.site1.info/search/search/?L=&tx_kesearch_pi1[sword]=YOU
TO
www.site2.org?index.php?id=243&L=&tx_kesearch_pi1%5Bsword%5D=YOU
www.site2.org/search/search/?L=&tx_kesearch_pi1[sword]=YOU
CAUTION
i only want to redirect ONLY if ...
www.site1.info?index.php?id=243
www.site1.info/search/search/
... and than redirect with the ongoing query (porting it to the site2.org
i tried unsuccessfully:
RewriteCond %{HTTP_HOST} ^(www\.)site1\.info/index.php?id=243$ [NC]
RewriteRule ^ http://www.site2.org/index.php?id=243%{REQUEST_URI} [NE,R=301,L]
You cannot match REQUEST_URI OR QUERY_STRING in RewriteCond %{HTTP_HOST}.
You can use this code:
RewriteCond %{HTTP_HOST} ^(www\.)site1\.info$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)id=243(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.site2.org/index.php?id=243%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)site1\.info$ [NC]
RewriteRule ^search/search/?$ http://www.site2.org/index.php?id=243%{REQUEST_URI} [NE,R=301,L]

Second Rewriterule not working correctly

I am trying to do two things here..
redirect http://site.com.au/brand.php?pBrand=THENORTHFACE to http://site.com.au/brand/the-north-face (this works fine)
redirect http://site.com.au/listingbrand.php?pBrand=THENORTHFACE to http://site.com.au/brand/the-north-face (this does not work, when redirecting listingbrand.php?pBrand=DOSH or pBrand=ATKM, they both point back to the first rewrite the-north-face).
How do i make the second rewrite work for each brand? Also, is it correct to repeat the rewrites for each brand?
RewriteCond %{QUERY_STRING} ^pBrand=THENORTHFACE$ [NC]
RewriteRule ^brand\.php$ /brand/the-north-face/? [R=301,L]
RewriteRule ^listingbrand\.php$ /brand/the-north-face/ [R=301,L]
RewriteCond %{QUERY_STRING} ^pBrand=DOSH$ [NC]
RewriteRule ^brand\.php$ /brand/dosh/? [R=301,L]
RewriteRule ^listingbrand\.php$ /brand/dosh/ [R=301,L]
RewriteCond %{QUERY_STRING} ^pBrand=ATKM$ [NC]
RewriteRule ^brand\.php$ /brand/all-the-kings-men/? [R=301,L]
RewriteRule ^listingbrand\.php$ /brand/all-the-kings-men/ [R=301,L]
You have this rule 3 times:
RewriteRule ^listingbrand\.php$ ...
Which is not using RewriteCond since RewriteCond is only applicable to very next RewriteRule. Actually you don't even need a separate rule since earlier RewriteRule can handle both brand.php and listingbrand.php using OR in regex.
Change your code to this:
RewriteCond %{QUERY_STRING} ^pBrand=THENORTHFACE$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/the-north-face/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^pBrand=DOSH$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/dosh/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^pBrand=ATKM$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/all-the-kings-men/? [R=301,L,NC]