Combined two mod rewrite htaccess rules into one - apache

How would I combined these 2 htaccess mod_rewrite rules into one using OR.
RewriteCond %{QUERY_STRING} ^(.*)%20(.*)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]
RewriteCond %{QUERY_STRING} ^(.*)%2B(.*)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]
Is this how?
RewriteCond %{QUERY_STRING} ^(.*)%20(.*)$ [OR]
RewriteCond %{QUERY_STRING} ^(.*)%2B(.*)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]
or is there even a shorter way than that?

You can use regex alternation:
RewriteCond %{QUERY_STRING} ^(.*)(?:%20|%2B|\s)(.*)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]

Related

htaccess redirect rule for multiple URL params

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]

.htaccess i want rewrite letters - numbers in letters/numbers

http:/example.com/word-123 --> http://example.com/word/123
i'm trying to do in this way but nothing happen
RewriteCond %{HTTP_HOST} ^([a-z]+)/([0-9]+)$
RewriteRule ^([a-z]+)-([0-9]+)$ [P]
OR
RewriteRule http://prink-it.adit.it/([a-z]+)-([0-9]+) [P]
From what I understand of your question:
RewriteEngine On
RewriteCond %{THE_REQUEST} GET\ /([a-z]+)\-(\d+) [NC]
RewriteRule ^ /%1/%2 [R=301,L]
RewriteRule ^([a-z]+)/(\d+)$ /$1-$2 [L]
I Solved in this way
RewriteCond %{THE_REQUEST} GET\ /([a-z]+)\/(\d+) [NC]
RewriteRule ^ /%1-%2 [R=301,P]
RewriteRule ^([a-z]+)-(\d+)$ /$1/$2 [P]

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]

combination of rewrite rules not working

RewriteEngine On
RewriteCond %{HTTP_HOST} !.com$ //redirect from .net and .org to .com
RewriteRule .* http://www.domain.com%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC] //Always use www.
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301]
RewriteRule ^/?article-(.*)$ article.php?id=$1 [L,QSA,NC] // redirect article-x to article.php?id=x
RewriteRule ^/?page-(.*)$ page?p=$1 [L,QSA,NC]// redirect page-x to page.php?p=x
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L] // add the .php to every request if the file type is missing
all the rules work fine except for adding www. to url. for example http://domain.com/page-1
doesn't get redirected to http://www.domain.com/page-1
%1 refers to a pattern captured with () in the preceding RewriteCond. You haven't captured any patterns in RewriteCond. Instead, you can just use %{HTTP_HOST} in the RewriteRule.
If these rules reside in .htaccess, RewriteRule will not contain a leading /, and will therefore never match if your pattern does. Remove the / and match on ^(.*)$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
Side note: don't forget to escape the . in .com. It will work unescaped, but the expression doesn't mean what you literally intend it to mean.
RewriteCond %{HTTP_HOST} !\.com$
The full set of rewrites all together should look like the following (I have removed all the leading / and /? expressions). I have tested all of this as working on my own system.
RewriteEngine On
RewriteCond %{HTTP_HOST} !\.com$ [NC]
#Use a capture group here, add [L]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteRule ^article-(.*)$ article.php?id=$1 [L,QSA,NC]
RewriteRule ^page-(.*)$ page?p=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [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]