Dynamic subdomains + defined subdomains via htaccess - .htaccess

I hate asking questions about mod_rewrite, but I can't seem to get these rules working properly. I had it setup and figured out once before, but a few years have passed and now it's just not playing nice and my mod_rewrite knowledge is back to uhhhh......
Essentially what I want to do is force www on the main domain, example.com. (or forcing no www would be even better, but I haven't remotely been able to get that working properly along with dynamic subdomains).
If my.example.com rules exist, then follow them accordingly
For anything else, ie: *.example.com, pass to another file to parse.
The rules I have currently handles forcing the www, and properly handles the dynamic subdomain, but completely ignores my.example.com rules, handling it as a wildcard subdomain. It seems I can get any one of my rules working properly, but when trying to get them working together it's just being a bastard. If anyone knows have to get this working properly it would be super appreciated.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# force www on main, force no www would be better.
RewriteCond %{HTTP_HOST} !^(.*)\.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# my.example.com defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1 [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]
# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.examples$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]

Try these rules:
# remove www on main
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
# my.example.com defined subdomain
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^.*$ - [S=3]
RewriteRule ^$ /mydirectory/index.php [L]
RewriteRule ^category/([^/]+)/?$ /mydirectory/index.php?category=$1 [L]
RewriteRule ^submit/?$ /mydirectory/process.php [L]
# *.example.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^([^/]+)/?$ /subdomainparse/index.php?subdomain=%1&fakedirectory=$1 [L]
The first rule will now redirect the +www.example.com* to example.com. And the third rule will catch any request to a non existing host.

Related

rewrite www. to non www without specifying the domain?

I was using this code to remove www from my domain:
# remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
My problem is that I usually use subdomains and it doesn't work for them. (www.sub.example.com).
How do I rewrite to remove www whatever the domain is and even if it has subdomains?
Try this:
#********** Remove www from address **********
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC]
This is a bit more correct than the other solution.
Edit:
I removed [L] as it isn't applicable here.
Though in most cases [NC] non-case sensitive is recommended.
You could probably use capture groups and combine with rewrites... (Completely untested guess)
# remove www
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ http://$1 [R=301,L]

Remove wildcard subdomains but not existing ones with .htaccess

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]

multiple errors, not redirecting, apache2/htaccess

Here is my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^. /archive/index.php [L]
going to domain.com will redirect me to www.domain.com
however, going to domain.com/2011/11/18/blog-title will show http://domain.com/var/htdocs/public_html/ instead in the browser URL.
My objective is any page at domain.com will redirect to www.domain.com
and wether I go to www.domain.com or domain.com /YYYY or /YYYY/MM or /YYYY/MM/DD will pass a PHP REQUEST_URI so I can get data from a MySQL database.
I originally copied the .htaccess file from WordPress but it doesn't seem to work properly since /var/htdocs/public_html appears in the browser URL bar.
This will do the trick:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Or this if you want a specific domain:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
The rules are from Drupal 7 and Drupal 6 respectively and have always worked well for me
EDIT
Looking at your code again I think the only problem is that you don't have a / between http://www.domain.com and $1. Other than that it's pretty much identical to the second example above which definitely works

Non WWW to WWW redirection using htaccess File

I am using Codeigniter-.
My domain currently does not redirect to www version.
For example if I type mydomain.com then it stays mydomain.com. I want to redirect it to www.mydomain.com.
If someone types mydomain.com/controller/method then it should be www.mydomain.com/controller/method.
Another problem: I already tried other solutions but the problem is when it redirects to www version, it automatically adds "index.php" in the URL. But when I type www in the domain name then it works fine, no "index.php" in the URL. This problem occurs only during the redirection.
Here is my .htaccess file (I've removed the redirection code)
RewriteCond $1 !^(index\.php|system|rpc_relay.html|canvas.html|robots\.txt)
RewriteCond $1 !^(sitemap\.xml|export)
RewriteRule ^(.*)$ /index.php/$1 [L]
Any help would be greatly appreciated.
To redirect from http:// to http://www. , and also remove the route file (index.php) in the url, put these lines on your htaccess :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|images|css|js|styles|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
the domain is :
domain.com
folder with direct access :
images|css|js|styles
hope this help
I've used the following before:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
To redirect domain.com to www.domain.com, you could use the following rewrite rule. Please replace domain.com with your own domain name.
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
I don't know why there are such complex RewriteRules answers, even though Gobhi has provided a nice generic solution (= whatever the domain name is, it works).
Here's my solution.
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule (.*)/index\.php$ $1/ [QSA]
</IfModule>

.htaccess Wildcard Subdomains

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.

Resources