David
David

Reputation: 23

How to make a .htaccess redirect with header and folders

How would the .htaccess code look like that redirects

http://www.example.com/folder to http://example.com/folder?

Upvotes: 1

Views: 1977

Answers (2)

anubhava
anubhava

Reputation: 785406

Insert this rule just below RewriteBase /:

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]

Upvotes: 0

Alexander Kludt
Alexander Kludt

Reputation: 863

The following will do what you need, i put comments in front of the lines to show what they do:

# activate rewrite engine
RewriteEngine On

# set the rewrite base (optional)
RewriteBase /

# if the http host has something in front of the domain name
# capture what is written after the domain name
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]

# if the above is true forward user with 301 redirect to the domain name without 
# www or anything in front and add what we captured in the line above
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

Upvotes: 1

Related Questions