Recently I have gone through my website with some SEO tools and they suggest that I remove my IP canalization using .htaccess file, but I don't know how to remove this using .htaccess file code.
What does this mean?
If that's the only domain that you host from your document root, then you can try adding these rules to the htaccess file in your document root:
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.neotericuk\.co\.uk$ [NC]
RewriteRule (.*) http://www.neotericuk.co.uk/$1 [R=301,L]
If there are other hosts that are also being hosted from the same document root, then you can match specifically for the IP address:
RewriteCond %{HTTP_HOST} ^neotericuk\.co\.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^1\.2\.3\.4$
RewriteRule (.*) http://www.neotericuk.co.uk/$1 [R=301,L]
where 1.2.3.4 is your server's IP address.
Related
I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
I have added a subdomain "*" like *.mydomain.com to my cPanel account. with a directory name subdomainsystem.
Now every subdomain works fine and loading script from subdomainsystem directory and if I go to www.mydomain.com it was showing main root script (main site). But after few days if I go to www.mydomain.com, it is being considered as a subdomain as well.
Here is my .htaccess in root:
RewriteCond %{HTTP_HOST} !^mydomain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).mydomain.com [NC]
RewriteRule ^([a-z0-9-]+)/$ index.php?subdomain=%2&menu=$1 [QSA]
Thanks.
You need to tweak regex to disallow www also before main domain:
RewriteCond %{HTTP_HOST} !^(?:www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ index.php?subdomain=%2&menu=$1 [L,QSA]
I have converted URL query to sub-domain, but web-page returns webpage not available.
This is my code in .htaccess, Do i have to do any more settings in apache configuration file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^user=([^&]+) [NC]
RewriteRule ^(index\.php)$ http://%1.example.com/$1? [L,NC,R=301]
Kindly Help.
Thanks
This can work. Your big problem isn't your .htaccess file, but your lack of DNS records for your subdomains. You need to specify a wildcard at the DNS level. You also need to setup Apache virtual hosts using a wildcard.
As for your .htaccess file, note that it will only match if index.php is present in the URL. To match all requests, like http://example.com/?user=foo, http://example.com/bar.php?user=foo, or http://example.com/baz/bar.php?user=foo, use this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^user=([^&]+) [NC]
RewriteRule ^(.*)$ http://%1.example.com/$1? [L,NC,R=301]
I have a EC2 instance and host several sites on the server. I can redirect different domains to their specific folders but I cannot seem to redirect subdomains.
Here is my code so far for www.domain1.com which directs to "folder1" and retains the "www.domain1.com" address in the address bar - This is working fine:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} domain1.com
RewriteCond %{REQUEST_URI} !^/folder1
RewriteRule ^(.*)$ folder1/$1 [L]
However I cannot find a solution for sub-domains - I've tried many solutions similar to this - this does not work:
RewriteCond %{HTTP_HOST} sub.domain1.com
RewriteCond %{REQUEST_URI} !^/folder2
RewriteRule ^(.*)$ folder2/$1 [L]
I want http://sub.domain1.com to point to "folder2" but retain sub.domain1.com in the address bar. One thing I want to avoid is using my very long EC2 instance static address "http://ec2-12-34-567-890.compute-1.amazonaws.com/" in the code if this is possible.
Thanks
Try changing the host part to have boundaries:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
and
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
The first rule is matching against the host sub.domain1.com because it contains a domain1.com
I'm relatively new to using .htaccess, and have never done any coding besides what I've read online. I'm using Bluehost, and I'd like to redirect my blog subdirectory to a subdomain. Example: I'd like to redirect www.example.com/blog to blog.example.com.
I already have code in place to always add www. to the beginning of my blog address in the root folder, but I don't know how to accomplish the above redirect by using code in .htaccess. Any help would be appreciated!
A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.
If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).
Edit 2
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
I wanted to add my two cents,
1) to answer the question above, this rewrite should fix it:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^/blog$ http://blog.example.com [R=302,L]
2) but, I think this is not enough by itself, you also need to change DNS, so that blog.example.com is pointed at the right server, and this can be done by a cname similar to this:
blog.example.com CNAME example.com TTL 1080
(not exactly how it will look, but use your DNS webinterface to set this up).
Have you tried this one?
RewriteEngine on
RewriteBase /
RewriteRule ^/blog/(.*)$ http://blog.subdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^check.domain.info$
RewriteCond %{REQUEST_URI} !^/check/
RewriteRule (.*) /check/$1
To redirect subdomain1 and subdomain2 and directory3 to a directory with HTTPS://, I use the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1.example.com [OR]
RewriteCond %{HTTP_HOST} ^subdomain2.example.com [OR]
RewriteCond %{HTTP_HOST} ^example\.com/subdomain3 [NC]
RewriteRule ^(.*)$ https://example.com/subdirectory/$1 [R=301,L]