Spandana Krishnan
Spandana Krishnan

Reputation: 323

How do I create rules in “.htaccess” for redirecting URLs?

I want following redirects to happen

example.com --> www.example.com/abc
www.example.com --> www.example.com/abc
example.com/abc --> www.example.com/abc
example.com/anyRubbish --> www.example.com/abc

But I'm confused with the difference between RedirectCond and ReWriteRule and RewriteBase, 'L' and 'R in [L,R 301] etc.

I'm writing rules for Apache Server.

Upvotes: 0

Views: 55

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

You can use the following rule :

 RewriteEngine on
 RewriteCond ℅{HTTP_HOST} !^www\. [NC,OR]
 RewriteCond ℅{REQUEST_URI} !^/ABC
 RewriteRule ^ http://www.example.com/abc℅{REQUEST_URI} [L,R]

Here is a short explanation:

The first condition rewrite cond ℅{HTTP_HOST} !^www\. [OR] checks if the http host header string isn't starting with www . The condition returns true if host host value is example.com . The second condition RewriteCond ℅{REQUEST_URI} !^/ABC tests the Uri . Checks if the Uri isn't /ABC . . This returns true if the URL is example.com/foobar and returns false if the URL is example.com/ABC OR flag tells mod-rewrite to execute one of the conditions , if one of the conditions in the above script is met then the rule gets applied.

The RewriteRule RewriteRule ^ http://www.example.com/abc℅{REQUEST_URI} [L,R] is applied according to the conditions , this will rewrite all requests to www.example.com/ABC.

Upvotes: 1

Related Questions