.HTACCESS Modification on Web host and Local host - regex

#Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
Currently I have this piece of code, and it works when I put it on my webhost online. But the thing is, I use my localhost (own pc) for development of the website before I put it online. So when I use it on my local host, the url goes to www.localhost and this unables me to reach my own website on my local host.
Is there any way, perhaps some type of if-statement that only put that piece of code to work when I actually put it online on a webhost and not when I have it on my local host.

Yes, there is an if-statement in htaccess. Take a look here:
http://httpd.apache.org/docs/trunk/mod/core.html#if
You can also see this:
HTACCESS RewriteCond without messing up localhost
RewriteCond %{HTTP_HOST} \.
As mentioned there, this should work for localhost check.

Related

How to redirect all subdomain urls to main domain

For some reason search engines are indexing my addon domains on my hosting. They should not do that.
For example I just found urls like
addondomain/maindomain.com
how to prevent this happening? How did search engines even find my addondomains?
What is the solution here? I tried this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com
RewriteRule ^(.*)$ http://www\.maindomain\.com [L]
but when I visit the url for example
addondomain/maindomain.com for example nothing happens?
Try to Change in .httaccess file. You can replace your rule with this rule::
RewriteCond %{HTTP_HOST} =shop.domain.abc
RewriteRule ^ http://www.domain.abc/? [R=301,L]
Using %{REQUEST_URI} will cause original URI to be copied in target. Trailing ? in target will strip off any pre-existing query string.
This answer is a further explanation to my comment on your question.
how to prevent this happening? How did search engines even find my
addondomains?
Google can easily find these subdomains on your site. To prevent this from happening, you can set a redirection with a 301 status code to inform Google that it should not index the addon domain. By doing this, Google will update its index as well.
This is a very common scenario with shared hosting and specially when you use CPanel. In Hostgator's support pages, you can see they have mentioned about this behavior.
Addon URL Example
For the primary domain abc.com, if you assign the addon domain 123.com
to the folder "123," the following URLs would be correct:
abc.com/123
123.abc.com
123.com
All three of these paths would access the same directory and show the
same website. For visitors going to 123.com, there is no evidence that
they are being routed through 123.abc.com.
https://support.hostgator.com/articles/cpanel/what-is-an-addon-domain
You can fix this by adding the following to your .httaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]
NC - match in a case-insensitive manner.
R - causes a HTTP redirect to be issued to the browser. When given as
R=301 it will be issued with a 301 status code which is required to
inform Google that their index should be updated accordingly.
L - Causes mod_rewrite to stop processing the rule set. In most
contexts, this means that if the rule matches, no further rules will
be processed.
===========================================================
Edit: Updated to add redirection to all domains, as requested in the comments.
To do this, you can simply check if the hostname is equal to your main domain, and if it's not, redirect it to the main domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteRule ^ http://www.maindomain.com [R=301,L]
Hope it helps :)

Apache Rewrite for images on subdomain

I have a webserver and a subdomain. All of my images are stored in /public/images within my site directory for www.mysite.com. However I have a separate site directory for testing beta.mysite.com however on this page with a different git branch all of my images are broken because I did not want to copy all of the images. Is it possible to say for all image requests or for all 404 requests try looking at mysite.com?
I have found an example on another questions
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^images/(.*)$ http://example.com/sub_ds/test/images/$1
But since I am rather new to mod_rewrite im not sure what is going on or how to manipulate it to work for me.
You can use this rule in root .htaccess of beta subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} =beta.example.com
RewriteRule ^(images/.+)$ http://example.com//public/$1 [L,NC,R=301]

URL Rewrite regex, can't seem to get it to not force the include of www.'s

I've got the Hellicon Tech URL rewrite ISAPI filter on my server. I've got a global file that dictates the settings across the server with the following.
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]
Basically it forces any request coming to the site to apply a "www." to prefix the URL that's being requested.
I've created a specific set of rules for a site on my server, but each time I attempt to modify the pattern to not force the www.'s it either breaks simply doesn't work.
I've narrowed this down to being specifically around the regular expressions, however I cant' seem to get it right.
Any suggestions on making this work would be greatly appreciated, still wanting to keep the https if the domain is requiring it, but trying to eliminate the www. prefix wont seem to work.
I can't test this with Hellicon's ISAPI, but have you tried something simpler?
RewriteCond %{HTTP:Host} !^www\. [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule ^(.*)$ http%2://www.%{HTTP:Host}/$1 [R=301,L]
remember to clear your browser's cache before testing, the 301 redirect is permanent and your browser will cache the redirected target.

Redirect all requests to index.php except subdomains

I'm using AltoRouter: (http://altorouter.com/) and I modified .htaccess as suggested in the instalation to:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
This is so that index.php can handle all the requests. My problem is that I am using addon domains in cpanel and I am having internal server errors when I try to access one of the domains associated with the addon domain.
Example:
My main domain is mainsite.com.
Then I have several sites:
site1.com that cpanel automatically associates with site1.mainsite.com and creates a folder mainsite.com/site1.com. So if I access site1.com I would see in browser site1.com but the content delivered would be the one inside the mainsite.com/site.com folder.
This works if I don't use the .htaccess rule but I need it for routing. If I have that rule I get internal server errors everytime I access site1.com (I assume that it's a conflict between cpanel rules and .htaccess).
How can I modify the rule so that it only affects maindomain and not subdomains? I am assuming that by doing this there would be no conflict and my problem would be solved.
I am really bad at .htaccess and regex but I am willing to learn if needed. I would still appreciate if you could point me to the right direction. (both in the idea and in good websites that can help me understanding this)
How can I modify the rule so that it only affects maindomain and not subdomains?
You can add a new condition based on host name:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?mainsite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

Getting a double slash when redirecting for a canonical hostname on Firefox only (Django)

I have a Django powered website, and I'm trying to solve the "canonical hostname" problem. I want www.example.com to redirect to example.com. I have tried both techniques found in the Apache documentation here (scroll down to Canonical hostnames).
I'm currently trying the mod_rewrite method, and I have this in a virtual host container:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*)$ http://example.com/$1 [L,R=301,NE]
This works for me, except for one case. In Firefox only, if I type www.example.com in a browser, it redirects and I see this in the URL bar: example.com// (note the 2 trailing slashes).
However, something like this will work correctly: www.example.com/news/ gets redirected to example.com/news/.
I only see this on the root URL in Firefox. It seems to work fine on Windows under Chrome, IE9, and Opera (maybe those browsers eat the double slash?). My Mac using friend says it is fine in Safari, but he also sees the problem in Firefox.
As far as Django settings go, I am using the default value of APPEND_SLASH=True. I don't know if Django has anything to do with it, but I've tried mod_rewrite rules like the above on static HTML sites before and it always seems to work.
Try to ignore the "/" by putting it into parenthesis, so, wheter it's here or not, the last part should work (and this is not $1 anymore but $2 in the redirection URL):
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(/?)(.*)$ http://example.com/$2 [L,R=301,NE]
Please tell me if it works.
This magically stopped happening, so I am going to assume there was a problem with Firefox at some point.