I have this rule for rewriting all subdomains to some URL, but I would like to edit it to rewrite all subdomains except subdomain "m".
RewriteCond %{HTTP_HOST} ^(.*)\.myweb\.cz
RewriteRule ^(.*)$ http://myweb.cz/adverts/%1/$1 [L,NC,QSA,R=301]
Thanks everyone.
You can add a negative lookahead in your condition to avoid matching m.:
RewriteCond %{HTTP_HOST} ^((?!m\.)[^.]+)\.myweb\.cz$ [NC]
RewriteRule ^(.*)$ http://myweb.cz/adverts/%1/$1 [L,R=301]
Try this htaccess
RewriteCond %{HTTP_HOST} !^m\.myweb\.cz$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.myweb\.cz$
RewriteRule ^(.*)$ http://myweb.cz/adverts/%1/$1 [L,NC,QSA,R=301]
Related
I am routing all non-www requests to the www-domain by using this RewriteCond and RewriteRule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]
But there is one certain subdomain that I want to exclude, e.g. blog.mydomain.tld. How do I have to modify my condition and rule in order to achieve this?
You can just add that subdomain in regex of your RewriteCond:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|blog)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
Did you try adding
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^blog\.
RewriteRule ^(.*)$ http://www.mydomain.tld/$1 [R=301,L]
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]
here is what I do and works good:
#redirect subdomains to controller
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^([a-zA-Z0-9-_]*)$ blog.php?blog_uid=%1 [L,QSA]
now I want to redirect
http://example.domain.com/post-123.html
to
blog.php?blog_id=example&post_id=123
but this not works:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^([a-zA-Z0-9-_]*)/post-([0-9]+)\.html$ blog.php?blog_uid=%1&post_id=%2 [L,QSA]
how do it? actually how redirect such subdomain request to a rewrite condition (it is not a real page).
Use this rule instead:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^post-([0-9]+)\.html$ /blog.php?blog_id=%1&post_id=$1 [NC,L,QSA]
Try this one.
RewriteEngine On
RewriteRule ^post-([^/]*)\.html$ /blog.php?blog_id=example&post_id=$1 [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
I am stuck on the follow htaccess situation.
I want to redirect pages to subdomains.
Url example right now;
http://domain.tld/user/foo
I need to redirect this to;
http://foo.domain.tld
The subdomain is genereated by wildcard.
I tried the follow without succes;
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.tld [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld?$
RewriteRule (.*) s/index.php?user=%1 [NC,QSA]
RewriteRule ^user/(.*) (.*) [R=301,L]
Regards,
Nick
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.tld [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld?$
RewriteRule (.*) s/index.php?user=%1 [NC,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.tld [NC]
RewriteRule ^user/([a-zA-Z0-9-.]+) http://$1.domain.tld/ [R=301,L]