I'm trying to create some redirects with .htaccess but I never manage to get it fully functional. Maybe someone here can help me.
What I need is:
htt开发者_开发百科p://domain.se
andhttp://domain.com
to redirect tohttp://www.domain.com
.I also need
http://domain.se/somefolder
,http://domain.com/somefolder
as well ashttp://www.domain.se/somefolder
to redirect tohttp://www.domain.com/folder
.
I've tried to accomplish this myself but all I end up with is errors about data not being sent.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# folder rewrite
RewriteRule ^somefolder$ folder [L]
# domain redirect
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =domain.se
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This is to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.
First rule will rewrite (internal redirect) requests to
/somefolder
to/folder
. If you need this to be 301 Permanent Redirect, then replace[L]
by[R=301,L]
Second rule will do domain redirect job. This rule will ONLY redirect if domain is
domain.com
ordomain.se
. If you want to have redirect from ANY domain name (that your webserver is configured can serve) towww.domain.com
then replace those 2 RewriteCond lines with this one:RewriteCond %{HTTP_HOST} !=www.domain.com
.
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
That should meet all of your requirements. All requests that are not www.domain.com
will be redirected to that domain, with the request URI intact.
精彩评论