redirect url with complex query string - .htaccess

I've tried using RewriteCond and RewriteRule to redirect a complex URL to a new page but without success.
The original URL:
index.php?option=com_k2&view=item&id=127&Itemid=353
or http://www.example.com/index.php?option=com_k2&view=item&id=127&Itemid=353
Have tried:
RewriteRule ^index.php?option=com_k2&view=item&id=127& Itemid=353$ http://www.example.com/somepage [R=301,L]
Also:
RewriteCond %{QUERY_STRING} index.php?option=com_k2&view=item&id=127&Itemid=353
RewriteRule http://www.example.com/somepage [R=301]
And:
RewriteCond %{QUERY_STRING} ^id=127&Itemid=353$
RewriteRule ^$ http://www.example.com/somepage [R=301]
What am I missing?

Try:
RewriteCond %{QUERY_STRING} (^|&)id=127&Itemid=353(&|$)
RewriteRule ^(?:index\.php|)$ http://www.example.com/somepage [L,R=301]

Related

301 htaccess redirects with 3 parameters to static urls

Im trying to redirect several urls with 3 parameters to different static urls with .htaccess but nothing working.
1.
http://olddomain.com/index.php?id_category=28&controller=category&id_lang=2
to
https://newdomain.com/page1/
http://olddomain.com/index.php?id_category=30&controller=category&id_lang=2
to
https://newdomain.com/page2/
http://olddomain.com/index.php
to
https://newdomain.com
I tried the below code but http://olddomain.com/index.php not going to https://newdomain.com :
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page2/? [R=301,L]
You need to have specific longer matches first and then have rules to remove index.php or domain redirect:
RewriteEngine On
# specific redirects with index.php as optional match
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page1/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page2/? [R=301,L,NC]
# remove index.php and redirect to newdmain
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^(?:index\.php/?)?(.*)$ https://newdomain.com/$1 [L,R=301,NC,NE]
Make sure to clear your browser cache before testing this change.
In case you are taking page's id from id_lang= variable then please try following rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules to redirect to link: https://newdomain.com/page1/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id_category=28&controller=category&id_lang=(\d+)\s [NC]
RewriteRule ^ https://newdomain.com/page%1/? [NE,R=301,L]
##Rules to redirect https://newdomain.com/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ https://newdomain.com [NE,R=301,L]

Htaccess www to non-www but keep the url intact

I think that this is rather simple, but I just can't wrap my head around it.
I have a domain like https://www.example.com
I want to rewrite it to https://example.com
That works. But I am also rewriting other urls like https://www.example.com/privacy
If I try to rewrite these too the user is always redirected to https://example.com/privacy.php
How do I prevent my server to show the actual filename instead of its rewritten url?
My code looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^checkout checkout.php [L]
RewriteRule ^imprint imprint.php [L]
RewriteRule ^privacy privacy.php [L]
RewriteRule ^terms terms.php [L]
RewriteRule ^allergy allergy.php [L]
RewriteRule ^nutrition nutrition.php [L]
RewriteRule ^order/([A-Z]*)?$ confirmation.php?lang=&id=$1 [L,QSA]
Any help is appreciated.
Regards
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(checkout|imprint|privacy|terms|allergy|nutrition)/?$ $1.php [L]
RewriteRule ^order/([a-z]+)/?$ confirmation.php?lang=&id=$1 [L,QSA,NC]
Make sure to test after clearing your browser cache. Problem appears to be due to your patterns not using end anchor.
I have combined your multiple similar rules into one rule.

htaccess language rediretion

I have a website, www.thesite.com and an alias www.lesite.com
i need a htaccess redirection:
I would like that, when I go to www.thesite.com/fr, it redirects me to www.lesite.com/fr
Likewise, when I go to www.lesite.com/en, I would like it to redirect me to www.thesite.com/en
I have tried different methods but i only succeed to create infinite loops !----
Options +FollowSymlinks
RewriteRule ^fr$ http://dev.mariage.ch/fr/ [L]
RewriteRule ^de$ http://dev.hortzeit.ch/de/ [L]
OR
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch\/de\/
RewriteRule (.*) http://dev.hortzeit.ch/de$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch\/fr\/
RewriteRule (.*) http://dev.mariage.ch/fr$1 [R=301,L]
You can't use path as part of RewriteCond for HTTP_HOST
instead of dev.hortzeit.ch/de you must use just host part dev.hortzeit.ch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch [NC]
RewriteRule ^de(/?.*)$ http://dev.hortzeit.ch/de$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch [NC]
RewriteRule ^fr(/?.*)$ http://dev.mariage.ch/fr$1 [R=301,NC,L]

.htaccess - 301 redirect with no query string, leave as is if query string

I put in a 301 redirect for my /index.php as recommended for SEO purposes. Unfortunately this prevents any params being accepted.
example.com/index.php should redirect to example.com
example.com/index.php?anything should stay at example.com/index.php?anything
example.com/index.php?f=bar&b=foo should stay at example.com/index.php?f=bar&b=foo
My attempted fix is below:
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s\/index\.php\s
RewriteCond %{QUERY_STRING} =""
RewriteRule . http://${HTTP_HOST}/ [R=301,L]
Turns out the answer for the scenario is a combination of Deadooshka's answer and a POST fix.
RewriteCond %{THE_REQUEST} \s\/index\.php\s
RewriteCond %{QUERY_STRING} =""
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

Htaccess Rewrite single query string

I'm having an issue with a rather old siye. I have some generic URL's with a query string, that i want to 301 redirect, but I don't want to blanket re-direct the urls. I want to choose where each query string is being redirected as there are alot of different categories within the site. For example:
I want to change:
index.php?_a=viewCat&catId=199
to:
/garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html
But ill want to change another catid to another URL of my choosing, completely different structure. The problem I'm having is with the code i've got, if I don't have a ? at the end of the destination url, it works, but appends the query string to the end, if I put it on the end, it doesn't redirect at all.
Code I'm using:
RewriteCond %{QUERY_STRING} ^_a=viewCat&catId=199
RewriteRule ^index\.php$ /garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html? [L,R=301]
Any help would be appreciated!
EDIT: The rest of my htaccess
RewriteEngine On
RewriteRule ^conservatory/(.*)$ /conservatory-furniture/$1 [R=301,L]
RewriteRule ^dining-room/(.*)$ /dining-room-furniture/$1 [R=301,L]
RewriteRule ^garden/(.*)$ /garden-furniture/patio-furniture/$1 [R=301,L]
RewriteBase /
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule prod_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewProd&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule info_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewDoc&docId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule tell_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=tellafriend&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule _saleItems(\.[a-z]+)?(\?.*)?$ index.php?_a=viewCat&catId=saleItems&%1 [NC,L]
If these are complete URLs, you could anchor the pattern at the start of the string
RewriteRule ^cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
this would prevent an URL, which has cat_ inside being rewritten to index.php?....
And since you don't use the trailing optional part, you could eliminate this too
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1&%1 [NC]
Another point is the RewriteCond with query string. If the query string is optional, you could remove the RewriteCond and modify the RewriteRules to
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
So, all these would become
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
RewriteRule ^prod_([0-9]+) index.php?_a=viewProd&productId=$1 [QSA,NC]
RewriteRule ^info_([0-9]+) index.php?_a=viewDoc&docId=$1 [QSA,NC]
RewriteRule ^tell_([0-9]+) index.php?_a=tellafriend&productId=$1 [QSA,NC]
RewriteRule ^_saleItems index.php?_a=viewCat&catId=saleItems [QSA,NC,L]

Resources