Valid apache mod_rewrite in .htaccess - apache

I am currently making changes to the .htaccess file to mod_rewrite a few URLs. I've done some reading and came up with the following.
RewriteCond %{HTTP_HOST} ^(www\.)?foobar\.net [NC,OR]
RewriteCond %{HTTP_HOST} ^foobar\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^shop\.foobar\.com [NC]
RewriteRule ^(.*)$ http://www.foobar.com/$1 [R=301,NC,L]
So my question is, is the above sufficient to redirect the following domains to www.foobar.com, while maintaining the trailing URL (eg. www.foobar.net/booya should go to www.foobar.com/booya):
www.foobar.net
foobar.net
foobar.com
shop.foobar.com

Yes, and actually, if you are not serving other, independent domains but those ones, and your new foobar.com is on a different server, you could just remove those RewriteCond.
On the other hand, if you are serving in the same server the new domain, and you want that all other possible domains to be redirected to www.foobar.com (which I think it may be your case), you could try instead:
RewriteCond %{HTTP_HOST} !(^www\.foobar\.com) [NC]
RewriteRule ^(.*)$ http://www.foobar.com/$1 [R=301,NC,L]
That way, you don't have to worry that you may be forgetting some other domain to redirect.

Related

Redirect HTTPS to HTP on Apache

I have an account with a webhost that uses Apache servers. The webhost's file structure uses subfolders for secondary domains of the primary account domain.
What do I need to add to this .htaccess file to redirect if someone types https:mysubdomain in the browser URL. I want to redirect from https to http, ie. http:mysubdomain.
RewriteEngine on
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
RewriteCond %{HTTP_HOST} ^myseconddomain\.myprimarydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.myseconddomain\.myprimarydomain\.com$
RewriteRule ^/?$ "http\:\/\/mysedonddomain\.com" [R=301,L]
Edit Update:
Thank you for suggestions. The approach of modifying the .htaccess file for the subdomain in the subfolder didn't work, even after clearing browser cache. What about modifying the .htaccess for the maindomain. I tried this but it didn't work either. Maybe my syntax?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^https:\/\/myseconddomain.com$
RewriteRule ^www.myseonddomain.com/ [R=301,L]
I have spoken at length with the webhost, Hostmonster, and all they could tell me was that the SSL certificate was working "correctly" - even thought it is associating with unrelated domain names that are not supposed to have any certificate. I guess that is what User82217 was saying, there is no other way than to purchase a wildcard SSL?
Edit Update: I tried putting this in the .htaccess of the maindomain and the seconddomain and nothing works to redirect from https to http when the user types https:// in front of mysecondubdomain.com in the URL
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^https
RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Anybody got any more ideas? Thank you.
To force HTTPs to HTTP then you can use the following in your .htaccess file:
#Force HTTP on everything
RewriteCond %{HTTPS} =on
RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
You didn't specifiy if you wanted to remove www or not, but on the assumption that you do, you can also remove that by including the following rule:
RewriteCond %{HTTP_HOST} ^www\. [OR]
Therefore checking if www is in the URL or not, so altogether using:
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Make sure you clear your cache before testing this.

Redirect a.com/a.html to b.com/a.html

I need to redirect a.com/a.html to b.com/a.html htaccess Apache. I have several subdomains that are not being redirected to the b.com(which is fine). and the example below is only the first domain that needs to be redirected. Three other domains are needed to be redirected to b.com as well with all of their subpages as a.com/a.html to b.com/a.html. Any hints?
RewriteCond %{HTTP_HOST} ^(www\.)?nexus21\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^tvlift\.com$ [NC]
RewriteRule ^ http://www.tvlift.com%{REQUEST_URI} [L,NE,R=301]
I would do each subdomain redirect separately. That way it is cleaner and more maintainable. If you are redirecting few known files then name them directly.
RewriteEngine on
RewriteRule "^/foo\.html$" "bar.html" [R]
If there are lots of file or you will be adding in future then use a pattern
RewriteEngine on
RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
See this page
http://httpd.apache.org

.htaccess for domain change with subdomains - apache + plesk

We're using a plesk based system and just recently changed domain names. I'd like to redirect all requests coming in to the old domain to the new. There are many question asked in a similar fashion but mine is a bit different. I'd like to ensure that all subdomains get routed to the same subdoamin on the new domain. I set up a generic htaccess in the docroot but for some reason it is also applying to all subdomains.
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
How can I make this more general so that subdomains also appropriately get routed? For bonus points, how can I route https requests to https and http to http.
I'd like to add that the rule transforms the first url to the second which is not desirable:
http://SUBDOMAIN.olddomain.com/somepath/somefile.php
http://newdomain.com/subdomains/SUBDOMAIN/httpdocs/somepath/somefile.php
The proper transform should create the following url:
http://SUBDOMAIN.newdomain.com/somepath/somefile.php
Lastly, this should work with wildcard subdomains.
Thanks in advanced!
Try:
# ignore "www."
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://%1.newdomain.com/$1 [L,R=301]
You'd want this before the rule that you already have.
And since you've edited your question, you want an additional internal rewrite for handling subdomains in general on your new site, this has nothing to do with the redirect. This is brand new functionality that is outside of the scope of a 301 redirect. You're going to need special rules to handle internal routing of subdomains:
RewriteCond %{REQUEST_URI} !^/subdomains/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.newdomain.com$ [NC]
RewriteRule ^(.*)$ /subdomains/%1/httpdocs/$1 [L]

apache mod rewrite for subdomain

On my webserver, I have one website located at example.com/site (in /var/www/site).
I want it to be accessed through site.example.com, and I'm aware I need to use mod-rewrite to enable this.
Is there a concise snippet I can use to do this? I need to make it extensible so other sites can be accessed this way as well. Thanks in advance.
If this is a matter of one specific subdomain: site, then you should explicitly rewrite just that one subdomain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L]
The second condition prevents an internal rewrite loop and making your URI look like /site/site/site/site/site/site/ etc...
This would go in your .htaccess file in your document root, /var/www/
If you really want to arbitrarily redirect any subdomain, you can try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]*)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)[^:]*:\1 [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
Here, the second condition serves the same purpose, it tries to make sure the request URI doesn't start with the subdomain match from the host. See https://stackoverflow.com/a/11508430/851273 for a better explanation.
First you need to configure your server to accept any subdomain for your domain example.com and redirect it to your virtual host that as well has to accept any subdomain. After that, you can use the following rule to rewrite that subdomain internally to a folder with the same name:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ %1%{REQUEST_URI} [L]

Apache Rewrite: *FULL* non-www to www permanent redirect

I'm having difficulty constructing a non-www to www permanent redirect. Here are the conditions I need to meet...
1.) Multi-domain support (multiple website domains pointed at the same directory on a server with their own dedicated copy of a database). This means using %{HTTP_HOST} and NOT a static domain name.
2.) ALL possible pathes (or at least standards compliant ones) need to redirect; this means directories, HTTP queries, etc.
3.) I'm using shared hosting, so I have access to the .htaccess file only.
Here is what I'm working with right now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^%{HTTP_HOST}
RewriteRule (.*) http://www\.%{HTTP_HOST}/$1 [R=301,L]
Details:
I'm testing with the browser cache disabled just in case.
I've removed other syntax during testing to see what replies work in their own right.
Here you go:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
It will also keep the URI scheme.
This here works for me (with my domainname)
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^%{HTTP_HOST}
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
The only difference I see is the matching pattern at the RewriteRule.