301 redirect with ampersand in link - .htaccess

I need to set up redirect 301 in .htaccess file but old link contain & signs. So, it is not works
My old link: /index.php?option=com_content&view=article&id=62&Itemid=75
so, I tried this:
RewriteCond %{QUERY_STRING} ^option=com_content&(.*)$
RewriteRule .+ / [R=301,L]
Can you help me with that?
Also I have this old link: /index.php?parametr1=services&parametr2=Recovery_Services&parametr3=Orthopedic_in_home_care

You need to use a question mark at the end of the Rewrite destination
RewriteCond %{QUERY_STRING} ^option=com_content&(.*)$
RewriteRule .+ /? [R=301,L]
? is important here to discard the old query string from the new url otherwise you will end up at /?oldquerystring

Related

.htaccess - remove one variable and 301 redirect, but leave others untouched?

I'm trying to remove one entire variable (but only if it's equal to 1). How can I do this? Basically I want to remove the string "&page=1". Here's what I mean:
If page=1:
301 redirect from BEFORE to AFTER
BEFORE: https://www.fubar.com/search.php?cs=tt&st=ss&page=1
AFTER: https://www.fubar.com/search.php?cs=tt&st=ss
If page = 2 or any other number EXCEPT 1 (nothing is changed or forwarded - no 301):
BEFORE: https://www.fubar.com/search.php?cs=tt&st=ss&page=2
AFTER: https://www.fubar.com/search.php?cs=tt&st=ss&page=2
I've tried some suggestions and searches, but nothing seems to be working...
Try with below rule,
RewriteEngine On
RewriteCond %{QUERY_STRING} !page=1$
RewriteRule ^ https://www.fubar.com/search.php?%{QUERY_STRING}&page=1 [R=301,L,END]
RewriteCond %{QUERY_STRING} !page=1$
RewriteRule ^ search.php?%{QUERY_STRING}&page=1 [L]
You can use the following :
RewriteEngine on
# if &page=1
#redirect to remove the &page perm
RewriteCond %{THE_REQUEST} /search\.php\?cs=tt&st=ss&page=1\s [NC]
RewriteRule ^.+$ https://www.fubar.com/search.php?cs=tt&st=ss [NE,L,R]
This will redirect your url from https://www.fubar.com/search.php?cs=tt&st=ss&page=1 to the same url but without the page perameter at the end (ie : https://www.fubar.com/search.php?cs=tt&st=ss ) .
Note that R is a 302 temporary redirect flag . I am using this just to test the rule and avoid browser caches. You can change the R to R=301 to make the redirection permanent .
I found a solution HERE: htaccess 301 redirect rule to remove part of a query string from URLs, but leave the rest)
Here's the code I ended up using:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)&?page=1(.*)$
RewriteRule ^/?search\.php$ /search.php?%1%2 [R=301,NE]

Redirecting based on query string

Hi all I need to redirect based on query string though htaccess in Apache.
I want that all the queries that contain jjj redirect to my homepage
I used the code below but its not working could you please help me¿
Thanks in advance,
Mauri
RewriteCond %{QUERY_STRING} ^jjj$
RewriteRule (.*) http://www.eventosbarcelona.com [R=301,L]
Your rule redirects example.com/?jjj to example.com/?jjj I guess, You are getting redirect loop error as both urls are same. By default, mod-rewrite appends old query strings to the destination url. You need to use a ? at the end of the Rewrite destination url to discard query string.
Try this :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^jjj$
RewriteRule (.*) http://example.com? [L,R]
This will redirect /?jjj to http://example.com/ .

301 Redirect with mod_rewrite

I have some SEO to do on my site.
I'd like that the page 'red-bags' point to the search page 'search.php?keyword=red bags'
In addittion I'd like that the old search page makes a 301 redir to the new red-bags.
RewriteBase /
RewriteRule ^red-bags /search\.php/keyword=red\sbags [NC,L]
RewriteCond %{QUERY_STRING} keyword=red\sbags
RewriteRule ^search.php http://www.mysite.com/red-bags [R=301,L]
The second rules not work. Please help.
Try changing the second rule to this:
RewriteCond %{QUERY_STRING} keyword=red%20bags
RewriteRule ^search.php http://www.mysite.com/red-bags? [R=301,L]
In the first line the space is changed to the urlencoded %20
The question mark at the end of red-bags strips off the query string, otherwise it would have automatically been appended to the new url.

301 Redirects problem - urls with ? and =

I'm new with 301 redirects through .htacces.
I can get simple redirects as
redirect 301 /test.html http://www.domain.com/test2.html
to work but I have some urls like this
redirect 301 /test.asp?Group=100 http://www.domain.com/test3.html
and for some reason these don't work.
Thanks.
Here is set of rules for URLs you have provided:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =group=113 [NC]
RewriteRule ^group\.asp$ http://domain.dk/til-born.htm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} =product=1136 [NC]
RewriteRule ^product\.asp$ http://www.domain.dk/til-born/bukser.html? [NC,R=301,L]
As you can see query string is matched separately to the page name. So .. for each of such redirects you need 2 lines: RewriteCond & RewriteRule.
The rule above will do EXACT match, which means /group.asp?group=113&param=value will not be redirected because query string is group=113&param=value which is more than just group=113.
To have such redirect working (when there are some optional parameters in query string) you have to modify it: RewriteCond %{QUERY_STRING} (^|&)group=113(&|$) [NC] -- this will match group=133 anywhere in query string (group=113 and group=11366 are still different, so no problems here).
This needs to be placed in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
The Redirect directive (as far as I know) matches only on the path, not querystring. Instead, use RewriteRule. The QSA instructs the rewrite engine to append the querystring onto the new redirected URL.
RewriteEngine On
RewriteRule ^test\.asp http://www.domain.com/test3.html [L,R=301,QSA]

301 Redirect Rule

Need help with an 301 htaccess redirect rule doing the following:
www.name.com/wordA/wordB/* to www.name.com/WordNEW/wordA/wordB/*
we are basically adding "WordNew".
I have three wordA and five wordB for a total of 15 path variations.
Spin this on for size:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/WordNEW [NC]
RewriteRule ^(.*)$ /WordNEW/$1 [L,R=301]
Broken down
RewriteCond %{REQUEST_URI} !^/WordNEW [NC]
Check the requested URI does not start (!^) with the same folder path (/WordNEW) as the final. If it does, you've already redirected once or you are already there. If you don't check then you can end up on a loop rewriting the path over and over again.
RewriteRule ^(.*)$ /WordNEW/$1 [L,R=301]
If it passes, grab the whole requested URI (^(.*)$) and prepend with the new folder path (/WordNEW/$1). Then flag this as the last rule (L) in the RewriteRule while redirecting under 301 (R=301).
if WordNEW is in a constant position my quick and dirty solution would be to split the url string on '/'. at index 1 I would prepend the string WordNEW/ . For a more feasible solution I will need a few moments to think.
Here’s a simpler rule:
RewriteRule !^WordNEW/ /WordNEW%{REQUEST_URI} [L,R=301]

Resources