I have a vHost in Apache and I want to rewrite all subdomains to the subdomain www.domain.tld where the tld-part should be the one, the user enters.
I already looked at the documentation of mod_rewrite, but at least I didn't understand :) I hope someone can exlain it to me.
The actual part in my vHost-Config ist the following,... but only matches the subdomain problem, but not the tld problem:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?domain\.de$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1domain.de%{REQUEST_URI} [R=301,L]
But I don't understand the %1 before my domain...
RewriteCond %{HTTP_HOST} ^(?!www\.)(?:[^.]+\.)?(domain\.[^.]+)$ [NC]
RewriteRule .* http://www.%1/$0 [R=301,L]
That redirects all requests where the subdomain isn't www, has 0 or 1 sudomain levels, domain is domain and has one level of tlds. If you also need, say .co.uk, it would need modification.
Related
I use rewrite condition to redirect website always to www. my code is:
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
I can't found on the internet how can i do request if in url '.com' i need it because the website must be also localy accessible.
For example: i found this, but i can't understand how can i implement it with my script.
Your current RewriteCond is correct for applying www. to website.com if it is not already present. To avoid the RewriteRule happening when working on localhost, you need an additional RewriteCond to check the host.
This is because the condition !^www\.example\.com$ matches any domain except www.example.com, which includes localhost.
# Only apply other conditions if not working on localhost
RewriteCond %{HTTP_HOST} !localhost [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
You can make it a little more dynamic in case you need to work with multiple domains (example.com, example.org) and transform each to www.example.com, www.example.org:
# If not on localhost
RewriteCond %{HTTP_HOST} !localhost [NC]
# and the domain does not begin www.
RewriteCond %{HTTP_HOST} !^www\.
# Redirect to apply the www. to HTTP_HOST
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Use caution when testing - your browser may cache old 301 redirects. You may need to change them to R=302 during testing and clear your browser cache. When you are satisfied it works, change it back to R=301.
That RewriteCond should already work for your case, the next rule is processed only if the condition matches, so that rule is not processed for localhost.
I am using two Domains which one of them is the Main Domain (http://www.domain.com) and the second is a Domain to shorten URL's (http://doma.in)
Forwards should be like this
http://domain.com -> http://www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
http:// doma.in -> http:// www. domain.com
RewriteCond %{HTTP_HOST} ^doma.in [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
http://sub.domain.com -> http://sub.domain.com (no configuration needed?!)
and now the important one
http://doma.in/VC7s98X -> should forward to its target without to be converted to http:// www.domain.com
I dont know how I can do this part.
Your second set of rules matches everything ((.*)) on the domain doma.in. To only match the bare domain without any trailing path, change the rule as follows:
RewriteCond %{HTTP_HOST} ^doma.in [NC]
RewriteRule ^/?$ http://www.domain.com/$1 [R=301,L]
That takes care of http://doma.in/VC7s98X not redirecting to http://www.domain.com. I'm assuming you have something in place to take care of http://doma.in/VC7s98X forwarding to its intended target ... if you're asking for help with that part, that's a much bigger question.
I am looking for a way to rewrite non-www-domains to www-domains, while at the same time not redirecting direct IP-requests.
I have multiple sites on the same server - that is: a default (virtual)host and one VirtualHost with a ServerName and multiple ServerAlias'es, which work perfectly. I prefer the domainnames to start with "www". So I have hacked the following code together, which works great:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It doesn't handle https, but the biggest problem is that requests to the server-IP are also rewritten from eg. "123.45.67.8" to "www.123.45.67.8". I could add the line below to solve that:
RewriteCond %{HTTP_HOST} !^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$
... but it is it effective? And what about IPv6?
Being no mod_rewrite-wiz, I have been trying to figure out how other people have solved this problem, but with no luck.
That's because your condition is only checking if it starts with www, try this instead (I left the optional https code):
RewriteCond %{HTTP_HOST} ^(yourdomain|thisdomain|thatdomain)\.com
#RewriteCond %{HTTPS} =on
#RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteRule .* http://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
I've read through a ton of posts and pages trying to figure this out. I have it mostly working. I have a .htaccess file setup and I'm trying to redirect (301) any page in my site from non-www to www version.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
It seems to work from the base url. However, it doesn't redirect for sub pages.
This works:
example.com -> www.example.com
This does NOT work:
example.com/foo.html -> www.example.com/foo.html
Any help would be much appreciated
I think you need to add a \ to escape the dot in your domain (line 2).
As in:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
...as seen here: http://www.hostingdiscussion.com/promotion-marketing/26083-301-redirect-non-www-www-vice-versa-good-search-engine-optimization-technique.html
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Here's my rule that work in my prod environment:
# If domain name without "www", add them:
RewriteCond %{HTTP_HOST} ^mydomainname\.(fr|com|net|org|eu) [NC]
# without www => force redirection:
RewriteRule (.*) http://www.mydomainname.%1$1 [QSA,R=301,L]
I am looking to map a secondary domain to a subfolder on my document root.
For example, if requests to the domain www.example.com map to my DocumentToot, then requests to www.exampletwo.com go to /sites/files/.
I am unable to accomplish a redirect from www.exampletwo.com/index.html to www.exampletwo.com/sites/files/index.html while making the URL still display www.exampletwo.com/index.html. Any ideas?
I believe you're looking for something like this:
RewriteCond %{HTTP_HOST} ^(www\.)?exampletwo\.com [NC]
RewriteRule ^/(.*) /sites/files/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^/(.*) http://www.exampletwo.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^(www\.)?exampletwo\.com [NC]
RewriteRule ^/(.*) http://www.exampletwo.com/sites/files/$1 [L,P]
The P flag uses the proxy module, therefore the url is not changed (no redirect) on the client.