1

I would like to add two 301 redirections in the .htaccess file, on an Apache server

1-One 301 redirection would be :

www.example.com/string1?fixed_text=anystringp=2

to :

www.example.com/string1?p=2

In other words, the following must be deleted from the url :

fixed_text=anystring

2- The other 301 redirection would be :

www.example.com/string1?fixed_text=anystring

to :

www.example.com/string1

In other words, the following must be deleted from the url :

?fixed_text=anystring

3- where string1 and anystring are variable alphanumeric strings, strings may include :

A to Z
a to z
0 to 9
/
.
-
&

string1 and anystring may have up to 200 characters

and where fixed_text is a fixed text (invariable text).

I thank you very much in advance for any help in this matter.

Patrick

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
user3278588
  • 81
  • 1
  • 12

2 Answers2

1

To redirect from

/string1/?fixed_text=foobar

to

www.example.com/string1

You can use :

RewriteEngine on


RewriteCond %{THE_REQUEST} \?fixed_text=([^\s]+) [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]

Or :

RewriteEngine on


RewriteCond %{QUERY_STRING} ^fixed_text=(.+)$ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Thank you very much to Olaf Dietsche and starkeen. I added the solution given by Olaf and it works like a charm. Patrick – user3278588 May 25 '16 at 10:06
1

If you want to strip fixed_text=... from the beginning of the query string, you must capture the part after it in a RewriteCond with QUERY_STRING.

RewriteCond %{QUERY_STRING} ^fixed_text=.*?(&(.*))?$
RewriteRule ^ %{REQUEST_URI}?%2 [L,R]

Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details. When everything works as expected, you may change the flag from R to R=301.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198