user3278588
user3278588

Reputation: 101

301 redirections in the .htaccess file, on an Apache server

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

Upvotes: 1

Views: 57

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74078

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.

Upvotes: 1

Amit Verma
Amit Verma

Reputation: 41219

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]

Upvotes: 1

Related Questions