.htaccess redirect page with variables - .htaccess

Please help to fix these redirects:
Redirect 1. category/?option=value => domain.com/category/page.html
RewriteCond %{QUERY_STRING} ^option=value(&.*)?$ [NC]
RewriteRule ^category/$ http://domain.com/category/page.html%1 [R=301,NE,NC,L]
Redirect 2. category/sub-%26-category/?option=value => domain.com/category/page1.html
RewriteCond %{QUERY_STRING} ^option=value(&.*)?$ [NC]
RewriteRule ^category/sub-\x26-category/$ http://domain.com/category/page1.html%1 [R=301,NE,NC,L]

You need to use ? in target URI to strip off any existing query string:
RewriteCond %{QUERY_STRING} ^option=value(&.*)?$ [NC]
RewriteRule ^category/$ /category/page.html? [R=301,NE,NC,L]
RewriteCond %{QUERY_STRING} ^option=value(&.*)?$ [NC]
RewriteRule ^category/sub-\x26-category/$ /category/page1.html? [R=301,NE,NC,L]
Make sure these rules are placed above other internal routing rules.

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]

How redirect query search result from domain which contains a folder?

I have this code in my .htacces file:
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ job\?search_keywords=%1 [NC,R,L]
And it works well for: example.com/?s=blabla
Now I would like to add that if the url contains /en/:
https://example.com/en/?s=blabla
change for https://example.com/en/work/?search_keywords=blabla
Somone could help me with it? Because my trying gives me ERROR 500.
You just need another rule to handle new URI pattern:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^$ /job?search_keywords=%1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^en/?$ /en/work/?search_keywords=%1 [R=301,L]
Note that \\? in your RewriteCond is redundant as QUERY_STRING variable doesn't contain ?

redirect URLs with query parameters

I need to add redirect from page http://www.mysite.ru/news/?PAGEN_1=1 to http://www.mysite.ru/news/, so I added in .htaccess file code below
RewriteCond %{QUERY_STRING} ^PAGEN_1=1 [NC]
RewriteRule ^news/$ http://www.mysite.ru/news/ [R=301,L]
but I get error "ERR_TOO_MANY_REDIRECTS". What's wrong and how to fix it?
This code works
RewriteCond %{QUERY_STRING} ^PAGEN_1=1 [NC]
RewriteRule ^news/$ http://www.mysite.ru/news/$ [R=301,L]

redirect url with complex query string

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]

Remove special character ? from url with .htaccess

I would like to redirect this url:
http://www.domena.pl/?tekst,123.html
to this
http://www.domena.pl/tekst,123.html
I just want to remove ? after first /, so I would like to redirect every url domena/?...... to domena/......
I was using this htaccess and it works for domena/testtekst,123, but I don't know how I should change it to work with special character ?
RewriteCond %{HTTP_HOST} ^www.domena.pl$
RewriteCond %{REQUEST_URI} ^/test(.*)$
RewriteRule ^test(.*)$ http://www.domena.pl/$1 [L,R=301]
This does not work:
RewriteCond %{HTTP_HOST} ^www.domena.pl$
RewriteCond %{REQUEST_URI} ^/?(.*)$
RewriteRule ^?(.*)$ http://www.domena.pl/$1 [L,R=301]
and this doesn't work too:
RewriteCond %{HTTP_HOST} ^www.domena.pl$
RewriteCond %{REQUEST_URI} ^/\?(.*)$
RewriteRule ^\?(.*)$ http://www.domena.pl/$1 [L,R=301]
You can use:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]

Resources