2

I need to add style=2 to all urls within a directory ONLY if the url does NOT already contain style=2.

Within the directory root, I have an .htaccess with the following:

RewriteCond %{QUERY_STRING} !(?style=|&style=)
RewriteRule ^(.*)$ %1&style=2? [R=301,QSA]

I know it's wrong. Can anyone help?

1

2 Answers 2

3

Change your code to this to make it work:

RewriteCond %{QUERY_STRING} !(^|&)style=2(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}?style=2 [R=301,L,QSA]
Sign up to request clarification or add additional context in comments.

7 Comments

This is properly tested code on all browsers so check your other .htaccess/code if that is causing too many redirects.
i want to check if user enters no paramters in url
You can use RewriteCond %{QUERY_STRING} ^$
i want to do something like if in url only domain i.e. example.com then i have to redirect like example.com/1/1
and if in url there is already parameters like example.com/1/2 then nothing redirect
|
0

Try this :

RewriteCond %{QUERY_STRING} !^(.*&)?style=
RewriteRule ^(.*)$ /$1?style=2 [L,QSA,R=301]

2 Comments

i want to check any keyword in URI
please make another question and explain a little more what you want to do and what you tried so that we can help you. Not here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.