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]
Related
I want to redirect requests on abc.com to abc.jp by adding this line to .htaccess file
RewriteRule ^(.*)abc\.com(.*)$ $1abc.jp$2
But it didn't work. So confusing...
Try using RewriteCond to match the domain, then redirect using RewriteRule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://abc.jp$1 [NC,L,R=301]
Sorry this has no doubt been asked multiple times before, I just want clarification that the following code will redirect any url on olddomain.com to the newdomain.com homepage not the equivalent url:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Also if I wanted any subdomain on olddomain.com eg.subdomain.olddomain.com to go to the homepage of newdomain.com what would I have to do? Can I use a universal selector or would I have to write a condition for each subdomain like so:
RewriteCond %{HTTP_HOST} ^subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
Both of attempts are not correct as first will redirect:
http://olddomain.com/foobar to http://newdomain.com/foobar
not to the homepage of newdomain. Same is the problem with 2nd rule.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.newdomain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.olddomain\.com$ [NC]
RewriteRule ^ http://subdomain.newdomain.com/ [R=301,L]
Let's say may main domain is www.mysite.com and that I'm using the following rewrite in my hataccess
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
All good so far! However, I also need other domains to redirect to specific pages on the site. For example, if somebody types www.mysite.nl they will be redirected to www.mysite.com/neatherlands. The issue is when I pop the below into my htaccess file I get a server error. I think this is because of the final rewrite. It's a pretty mad set-up
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.nl$ [NC]
Rewriterule ^(.*)$ http://www.mysite.com/netherlands/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.be$ [NC]
Rewriterule ^(.*)$ http://www.mysite.com/belgium/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.co.\uk$ [NC]
Rewriterule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
Any suggestions about what to do?
i need to redirect all requests of form http://static.name.ext/first/second/etc
to http://www.name.ext if first segment is not "assets"
so keep requests of form http://static.name.ext/assets/etc redirect others
i tried with
RewriteCond %{HTTP_HOST} ^static\. [NC]
RewriteCond %{REQUEST_URI} !/assets [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [R=301,L]
but %{HTTP_HOST} returns the subdomain and i don't want that
Try:
RewriteCond %{REQUEST_URI} !/assets [NC]
RewriteCond %{HTTP_HOST} ^static\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%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]