I've used the following to redirect domain.tld and *.domain.tld to www.domain.tld (except subdomain dev.domain.tld). But this doesn't seem to work for domains containing a dash like dom-ain.tld. Why?
RewriteCond %{HTTP_HOST} !^[www|dev]\.* [NC]
RewriteRule ^(.*) http://www\.%{HTTP_HOST}/$1 [R=301]
Your regex is actually incorrect.
Replace your rule with this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|dev)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Related
I'd like to redirect all domains like join.domain.com to signup.domain.com including all URI Params.
I'm not quite sure where to start with this as this should be one rule that blankets all join.* domains on my server.
RewriteEngine On
RewriteCondition %{http_host} ^join$ [NC]
RewriteRule ^(.*)$ http://signup.$1/$ [R=301,L]
Try his rule:
RewriteEngine On
RewriteCondition %{HTTP_HOST} ^join\.(.+)$ [NC]
RewriteRule ^(.*)$ http://signup.%1/$1 [R=301,L]
i would like to redirect 3 different urls, all in one htaccess file. But at the moment it isn't working and only the first rule is always triggered.
I would like to redirect the following:
service.abc.info to www.abc.de
service.abc.info/feedback/abc/ to feedback.abc.info/abc/
service.abc.info/feedback/def/ to feedback.abc.info/def/
For 2. and 3. I also need to redirect the complete Request URI.
Here is my .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule .* http://www.abc.de [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info/feedback/abc/$ [NC]
RewriteRule .* http://feedback.abc.info/abc/{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info/feedback/def/$ [NC]
RewriteRule .* http://feedback.abc.info/def/{REQUEST_URI} [R=301,L]
The %{HTTP_HOST} variable holds the contents of the Host: request header, which just contains a hostname. No URI paths. You'll need to match the paths in the pattern of the rewrite rule, additionally, since the first rule is a superset of the other two, you need to move that to the end.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule ^/?feedback/abc/(.*)$ http://feedback.abc.info/abc/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule ^/?feedback/def/(.*)$ http://feedback.abc.info/def/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule ^(.*)$ http://www.abc.de/$1 [R=301,L]
I'd like to redirect all URLs not containing www to www
I used this:
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*) http://www.example.com$1 [QSA,L,R=301]
Now I need one subdomain not to be redirect like this, or I get an error.
So I'd like to redirect any URL that does not begin with sub or with www to www.example.com
How can I do this ? thanks
You need a second RewriteCond. You can apply as many as you like to a RewriteRule.
Assuming anything that is not sub.mydomain.com needs to be www.mydomain.com, here is your code:
RewriteCond %{HTTP_HOST} !^sub.mydomain.com$
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*) http://www.mydomain.com/$1 [QSA,L,R=301]
But you can simplify this further using the pipe (|) character in Regex:
RewriteCond %{HTTP_HOST} !^(sub|www).mydomain.com$
RewriteRule ^(.*) http://www.mydomain.com/$1 [QSA,L,R=301]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This works for all domains, excluding any subdomains. have fun.
I tried the answer by Scott S but it didn't work, so I modified to use the one I was using for general 301:
RewriteCond %{HTTP_HOST} ^domain.com
RewriteCond %{HTTP_HOST} !^subdomain.domain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
And it works like a charm
Hello!
I'm trying to set up my .htaccess file for wildcard subdomains, but I really have no clue how to do that.
I have "domain2" pointing to "domain1" as an alias, which is working perfectly, this is the code I'm using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^(.*\.?)domain2.co\.cc$ [NC]
RewriteRule (.*) http://%1domain1.co.cc/$1 [R=301,L]
I found the www redirect here btw: Optimize htaccess Wildcard Subdomain Code
Now, what I want is all non-existent subdomains to get removed and the ones that exist (like "blog.domain1.co.cc" to stay.
I hope someone can help me with this. Thanks!
RewriteEngine On
#no longer needed
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#don't redirect blog.example.com, forum.example.com and example.com
RewriteCond %{HTTP_HOST} ^((blog|forum)\.)?example\.com$
RewriteRule .* - [L]
#redirect the rest (including www.) to example.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Try adding the following to your htaccess file.
#if these lines already exist, skip them
RewriteEngine On
RewriteBase /
#if its not www or discussions subdomain
RewriteCond %{HTTP_HOST} !^(www|discussions)\.domain1\.co\.cc$ [NC]
#redirect to www domain
RewriteRule .* http://www.domain1.co.cc%{REQUEST_URI} [R=301,L]
For the following question
redirects subdomains and subdirectories to the other domain, just like that: forum.old.com/thread/12038213 --> forum.new.com/thread/12038213
Try
RewriteEngine On
RewriteBase /
#if domain is old.com
RewriteCond %{HTTP_HOST} ^(.+)\.old\.com$ [NC]
#redirect to new.com
RewriteRule .* http://%1.new.com%{REQUEST_URI} [L,R=301]
I'm trying to implement a solution using .htaccess and wildcard subdomains so that
http://subdomain.example.com is mapped to http://example.com/index.php/accounts/subdomain/. My rules look something like:
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*/) /index.php [PT,L]
Which works, but disregards everything else. When I try appending anything to the rule e.g:
RewriteRule ^(.*/) /index.php/hello [PT,L]
I get a 500 internal server error. How do I get this working?
You probably need to exclude the index.php from your rule:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]
This is an adaptation of the code I use to redirect subdomains on my own site. I make no claims to it being best practice but it works;
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.example$ [NC]
RewriteRule ^.*$ http://www.example.com/index.php/accounts/%1/ [R=301,L]
Try changing your RewriteRule to
RewriteRule ^/(.*)$ /index.php/accounts/%1/$1 [PT]
That will rewrite the URL to one that includes the subdomain and the original request URI.
EDIT: maybe it needs to be
RewriteRule ^(.*)$ /index.php/accounts/%1/$1 [PT]
as mentioned in the comments.