Stuck on RewriteRule in htaccess - apache

I am stuck in redirecting my urls. I think i am almost there but can't oversee what i am missing. When i test the redirect is goes to the correct url, but sticks the old url behinde the new url.
This is where i got stuck:
RewriteCond %{QUERY_STRING} lang=uk&page=page
RewriteRule ^(.*)$ domain/page/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=fr&page=page
RewriteRule ^(.*)$ domain/page/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=uk&page=about
RewriteRule ^(.*)$ domain/about/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=uk&page=about
RewriteRule ^(.*)$ domain/about/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=uk&page=news
RewriteRule ^(.*)$ domain/info/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=fr&page=news
RewriteRule ^(.*)$ domain/info/ [L,R=301]
I am also scared i get stuck with a big htaccess file if i do all redirects individual. Would that slow things down?
Lang=nl, uk, us, fr all go to the same page.
not all page= got the same pagename after the change.
Thanks in advance,

You can have specific redirects first where page name is not same as target followed by a single rule where query parameter page has same value as the target URI. So your code can be shortened to these 2 rules only.
RewriteEngine On
RewriteCond %{QUERY_STRING} lang=(fr|uk|us|nl)&page=news
RewriteRule ^ /info/? [L,R=301]
RewriteCond %{QUERY_STRING} lang=(fr|uk|us|nl)&page=([^&]+)
RewriteRule ^ /%2/? [L,R=301]

Related

Rewrite rules with multiple query string

i'm new on htaccess. I'm working on a website with multiple query strings.
I need to rewrite urls of different query strings like:
/path_to_website/file.php?product=var1&country=0&pag=1 to /path_to_website/var1/
/path_to_website/file.php?product=var1&country=0&pag=var2 to /path_to_website/var1/var2
/path_to_website/file.php?product=var1&country=var2&pag=1 to /path_to_website/var1/var2/
/path_to_website/file.php?product=var1&country=var2&pag=var3 to /path_to_website/var1/var2/var3
As you can see, the problem is between 2 and 3 rewrite because they have the same number of parameters (if the problem is this). I've done this on htaccess file:
###BLOCK 1#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=0&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
###BLOCK 2#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=0&pag=$2&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
###BLOCK 3#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=$2&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/? [R=301,L]
###BLOCK 4#######
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /path_to_website/modules/file.php?product=$1&country=$2&pag=$3&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&(.*)=(.*)&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /path_to_website/%2/%4/%6/? [R=301,L]
How can I fix this? Because everything works except when I search for product and country !=0 the engine stops on block 2.
Please someone help me because I'm stuck on this problem.
Thanks a lot!
Edit
Thanks #RavinderSingh13 for your answer. I tried with your rules, like this:
RewriteCond %{QUERY_STRING} ^$
############your rule###########
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&country=0&pag=1$
RewriteRule ^.*$ /path_to_website/%2/? [R=301,L]
The rewrite with this rules is path_to_website/product/, but the page doesn't work with "ERR_TOO_MANY_REDIRECTS". I tried also with ONLY your rule:
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
In this case, the rewrite don't work at all, just print the query string. I also tried with the rew=1 key, like this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w-]+)/?$ /aziende_agrarie/modules/risultato_ricerca.php?
prodotto=$1&provincia=0&pag=1&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&provincia=0&pag=1$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /aziende_agrarie/%2/? [R=301,L]
The rewrite works with path_to_website/product, but when i change the page on 2, the browser url is path_to_website/product/?product=var1&country=0&pag=2. So I insert the rewrite for product/2/, and the rewrite for "file.php?product=var1&country=0&pag=var2" is:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ /aziende_agrarie/modules/risultato_ricerca.php?
prodotto=$1&provincia=0&pag=$2&rew=1 [L]
RewriteCond %{QUERY_STRING} ^(.*)=(.*)&provincia=0&(.*)=(.*)$
RewriteCond %{QUERY_STRING} !^.*rew=1.*$
RewriteRule ^.*$ /aziende_agrarie/%2/%4/? [R=301,L]
An so on. I miss something? Thanks a lot for your help :)
Based on your shown samples, could you please try following Rule sets in your .htaccess file. Also please make sure you clear your cache before testing your URLs.
RewriteEngine ON
##For file.php?product=var1&country=0&pag=1
RewriteRule ^([\w-]+)/?$ file.php?product=$1&country=0&pag=1 [L]
##For file.php?product=var1&country=0&pag=var2
RewriteRule ^([\w-]+)/([\w-]+)(?!=/)$ file.php?product=$1&country=0&pag=$2 [L]
##For file.php?product=var1&country=var2&pag=1
RewriteRule ^([\w-]+)/([\w-]+)/$ file.php?product=$1&country=$2&pag=1 [L]
##for file.php?product=var1&country=var2&pag=var3
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)$ file.php?product=$1&country=$2&pag=$3 [L]
Edit
I had a GET request with form submit and href with custom paginator. I solved the problem. The problem was on href because i left the query string like "path_to_website/?product=var1&country=var2&page=1" etc. I resolved this replacing the query string href with "/product/country" for pag1 and so on for the other href. On the submit event, i left my custom block 1 and block 3 rules and for paginator href i wrote your rules for rewrite.

Rewrite Condition with Special Case for Homepage

I switch my Business from one Domainname to another and want to use a 301 Redirect/Rewrite to keep the Google Ranking.
The special case is, that the Startpage and One other Page should go to a new Subpage on the new Domain, everything else should stay the same because of the structure of the new Domain is the same like the old one.
I tried several kinds of RegExp and another order. Maybe I misunderstand the path to a solution completly.
Maybe I should do a simple 301 Redirect on the Old Webpage and try to look into the referer on the New Apache Configuration and make an internal rewrite to /catvilla/
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.old.co.nz$ [NC]
RewriteRule ^(.*)$ https://www.new.co.nz/oldname/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.old.co.nz/$ [NC]
RewriteRule ^(.*)$ https://www.new.co.nz/oldname/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.old.co.nz/pricelist/$ [NC]
RewriteRule ^(.*)$ https://www.new.co.nz/oldpricelist/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.old.co.nz/ [NC]
RewriteRule ^(.*)$ https://www.new.co.nz/$1 [L,R=301]
The Apache Config in my VirtualHost right now, redirect everyging to www.new.co.nz/oldname all the time, eben for example for http://www.old.co.nz/webcam, what should go to www.new.co.nz/webcam.
Frank
So far I see the following is working:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^(.*)$ https://www.new.co.nz/oldname/ [L,R=301]
RewriteCond %{REQUEST_URI} ^/pricelist/$ [NC]
RewriteRule ^(.*)$ https://www.new.co.nz/oldpricelist/ [L,R=301]
RewriteRule ^(.*)$ https://www.new.co.nz/$1 [L,R=301]

Get utl parameters / URL rewriting

I'd like to use URL rewriting to have "virtual" subdomains. I already created wildcard DNS entry.
I want subdomain to be added as GET parameter.
Example:
http://mysubdomain.domain.com/index.php
would call:
http://www.domain.com/index.php?subdomain=mysubdomain
and
http://mysubdomain.domain.com/page.php?parameter1=parameter
would call:
http://www.domain.com/page.php?parameter1=parameter&subdomain=mysubdomain
This is my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+).domain.com$ [NC]
RewriteRule ^$ /index.php?subdomain=%2 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+).domain.com$ [NC]
RewriteRule ^([A-Za-z0-9_.]+)$ /$1 [L]
It works but I cannot get subdomain value, when calling other page than index.php, if I call http://subdomain.domain.com/page.php I loose subdomain parameter, I tried:
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+).domain.com$ [NC]
RewriteRule ^([A-Za-z0-9_.]+)$ /$1&subdomain=%2 [L]
But I got Internal error.
THis rule:
RewriteRule ^$ /index.php?subdomain=%2 [L]
needs a QSA flag:
RewriteRule ^$ /index.php?subdomain=%2 [L,QSA]
and this rule:
RewriteRule ^([A-Za-z0-9_.]+)$ /$1 [L]
needs the QSA flag and the proper backreference:
RewriteRule ^([A-Za-z0-9_.]+)$ /$1?subdomain=%2 [L,QSA]

RewriteCond www and non www to subpage

I'm a newbie and the question is simple, I want mysite.eu and www.mysite.eu redirect to a subpage of their website,
but I can't get it to work for both because when I add something like this I got stuck in a loop so how can I fix this that both wil direct to that subpage without getting a loop.
My example:
RewriteCond %{HTTP_HOST} www\.mysite\.eu [NC]
RewriteRule . http://www.mysite.eu/page?page=webshop_fixol&lng=1 [L]
RewriteCond %{HTTP_HOST} mysite\.eu [NC]
RewriteRule . http://www.mysite.eu/page?page=webshop_fixol&lng=1 [R=permanent,L]
Why do you have the . there? sure you did not just want this:
RewriteCond %{HTTP_HOST} www\.mysite\.eu [NC]
RewriteRule ^/$ http://www.mysite.eu/page?page=webshop_fixol&lng=1 [L]
RewriteCond %{HTTP_HOST} mysite\.eu [NC]
RewriteRule ^/$ http://www.mysite.eu/page?page=webshop_fixol&lng=1 [R=permanent,L]
It can be done in a single rewrite rule like this:
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.eu$ [NC]
RewriteRule . page?page=webshop_fixol&lng=1 [L,QSA]
^(www\.)?mysite\.eu$ will match both sites www and non www.

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]