how to create subdomain by .htaccess on rewrite directory - php

I have created a subdomain with this .htaccess; it is working fine on my www.example.com domain but not on www.example.pk. I do not understand where the problem is.
www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([-a-zA-Z0-9]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?index\.php$
RewriteRule ^(.*)/*([0-9]+)*$ index.php?r=list&source=product&product_get=%1&page_page=$1 [NC,QSA,L]
www.example.pk
RewriteCond %{HTTP_HOST} !^www\.example\.pk$ [NC]
RewriteCond %{HTTP_HOST} ^([-a-zA-Z0-9]+)\.example\.pk$ [NC]
RewriteCond %{REQUEST_URI} !^/?index\.php$
RewriteRule ^(.*)/*([0-9]+)*$ index.php?r=list&source=product&product_get=%1&page_page=$1 [NC,QSA,L]
I created www.example.com domain then the subdomain with cpanel *.example.com. Is it necessary or not?

When you work with subdomains, you need to configure your DNS correctly to direct subdomains to your server. The subdomain-feature of cpanel might have done this for you, or it already contained a wildcard cname dns record. DNS records are usually cached, but I am unsure if this is the case for non-existing records too.
Does the subdomain already resolve to my server?
First check if your subdomain already resolves to an ip. Open a terminal and type ping yoursubdomain.example.com (obviously you need to replace this with your actual domain). If it can't be resolved to an ip, then there is probably a problem with your DNS record. You can do that online if you want here or on several other, similar, sites.
Editing your dns
If your host gives you an opportunity to edit the DNS records manually, you'll need to know the following:
There is 1 A-record in your dns. This is how the name is translated to an ip.
Any subdomains are linked to that record using CNAME-records.
You can either add a wildcard-subdomain (which will translate any (un)thinkable subdomain to your site) or a specific subdomain to your dns. You do so by adding
CNAME (from) *.example.com (to) example.com
The TTL (time to live) is how long a record can be cached by others. It will, at most, take TTL to get everyone in the world to 'notice' changes you make in a record of your dns. This site gives some more information about that.
.htaccess
I am not sure what kind of urls you want to rewrite, but there are a couple of things you can change:
%{REQUEST_URI} always starts with a /. There is no need to add a ? behind it in your condition.
For readability, if you use %1-type-of-backreferences, put the corresponding condition directly above it, even if it turns out they are not overwritten by other conditions without capture groups.
^(.*)/*([0-9]+)*$ doesn't make much sense to me. It matches every url, because everything after (.*) is optional, and can be there an infinite amount of times. I doubt this is what you wanted it to be.
RewriteCond %{HTTP_HOST} !^www\.example\.pk$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} ^([-a-zA-Z0-9]+)\.example\.pk$ [NC]
RewriteRule ^(.*)/*([0-9]+)*$ index.php?r=list&source=product&product_get=%1&page_page=$1 [NC,QSA,L]

Related

Codeigniter 4 default .htaccess file query

This might sound like a stupid question to some, but I've only just noticed it while trying to implement an SSL certificate to my site.
There's a default value in the 'out of the box' .htaccess file:
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
Am I right in thinking this code forces the removal of the www. part of the canonical links on my website?
If so - is this really best practice? Is that why the base_url example in the Config/App.php is http://example.com?
Secondly, as I mentioned, I'm trying to add this code to the .htaccess file to implement the SSL certificate and force https for every URL - but it's causing an error if I use www. (whereas it didn't cause an error before) and my speed tests are indicating redirects galore which is slowing everything down:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Is anyone able to point me in the right direction with the correct canonical link structure for the base_url in the Config/App.php file, whether I need to alter (or scrap) the first snippet of code, and how I can force https to work with my SSL certificate (and www. in my URLs).
I would much rather my URLs had the structure of https://www.example.com as opposed to https://example.com
Am I right in thinking this code forces the removal of the www.
Yes, but only for HTTP (not HTTPS) requests, as governed by the first condition %{HTTPS} !=on.
If you are implementing HTTPS then you should remove the first condition and change the RewriteRule substitution string to redirect to https://.... But if you are wanting to redirect to www then you'll need to reverse the logic also:
# Redirect "example.com -> www.example.com"
# (In fact, redirect hostname that does not start "www.")
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Although, this particular method (as I've implied in the comment) will not necessarily work if you have other subdomains, unless you want all subdomains to have www sub-subdomains?!
Note that this is an external "redirect", not an internal "rewrite" as you'd stated in the comment.
If so - is this really best practice?
In terms of SEO or from a technical perspective? In terms of SEO there is no difference. Using a www subdomain can arguably have some technical benefits (isolating cookies and staging sites, etc.) - although this is mostly a matter of opinion and depends on your environment. It is really up to you. For some domain names, using a www subdomain just looks cumbersome.
But what is important is that you choose one or the other and redirect to the canonical URL in order to avoid potential duplicate content issues.
Using the domain apex (ie. no www subdomain) is simply CodeIgniters default.
force https for every URL - but it's causing an error if I use www.
To clarify, the SSL cert you implement must include both the domain apex, ie. exmaple.com and the www subdomain, ie. www.example.com. Otherwise, you will naturally get browser security warnings when requesting the "other" (non-SSL) hostname.
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This code is generally OK to force HTTP to HTTPS (providing the SSL cert is installed on your application server, ie. you're not using a front-end SSL proxy or non-standard implementation). However, the order you put this rule in relation to the rule above will depend on whether you intend to implement HSTS or not.
If you are intending to implement HSTS then you will need to redirect to HTTPS on the same host first, before the redirect to www. This will result in an unavoidable double redirect when requesting the non-canonical http://example.com/ (but that is not "bad").
For example:
# 1. Redirect to HTTPS on the same host
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 2. Redirect to non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If, however, you are not intending to implement HSTS then you can reverse the two rules above and get at most one redirect for any non-canonical URL request.
You need to update the CodeIgniter base_url to match your preference of www or not.

Manage two domains pointing to one hosting with htaccess

Well, I want to know somebody me to explain, if possible, how could I manage one hosting to have two domains pointing to it.
My purpouse is to have two folders, and manage it with htaccess.
I have two parked domains in Hostinguer, now I want to know how can I make that the htaccess only allow one folder to one domain and the other folder to another domain, and take this to all the directories, because now, I can access my webpage by the two domains and I want to know how can I solve it.
My first try was this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|)\.domain\.com$ [NC]
RewriteRule ^ - [F]
I know more or less what is every thing, there is a little bit of Regex and some instructions that says how it has to works.
Let's say something like that in PHP:
if($_SERVER['SERVER_NAME'] == "www.domain.com") //There is an regex I know
exit; //Or, in this case throw a 403 error.
But, for example, I don't know how can I apply this action to all the subdirectories, avoiding this two-three lines to every .htaccess in every folder.
And also, It didn't work as I expected, because, it throw me a 403 error if I visit it from my domain.com, but also, it throw me a 403 error if I visit it from my subdomain: http://ikillnukes.hol.es/fapi (I will leave it running all the night to prove it)
Note: I don't have access to the Apache, so, don't suggest me to make an virtualhost because I can't. ;-)
EDIT:
I have now:
domain1.com, domain2.com and sub.domain1.com
And those folders:
root
domain1 //This is created by me
sub //This is automatically created by Hostinger, and I can't change the name or the path
domain2 //This is created by me
So, there is any problem by doing what #Walf suggested to me, but I have the problem of the subdomains and the .htaccess #Walf provided to me is a little bit compilcated to understand (my fault), so, what can I should try?
So, any help to me explaining how to approach this would be fantastic!
Thanks in advance!
Your question is probably a duplicate but I can't find one that uses adjacent dirs (as I think you're describing), nor one with a solution I'd use, myself. Try this in your root .htaccess:
RewriteEngine on
# capture the original request so you never have trouble with (un)escaping
RewriteCond %{THE_REQUEST} \S+\s+(\S*)
RewriteRule ^ - [E=REQ:%1]
# ensure the domain goes to a dir with the same name (ignoring www)
# get domain
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
# see if first dir in url doesn't match domain
RewriteCond %1 =!^(?>([^|]+)\|\1)$
# capture first dir (if any) to check against above conditions and pass through as new url if not prefixed with domain dir
RewriteRule ^[^/]* %1%{ENV:REQ} [NE,DPI,PT]
Your dir structure would then be
docroot/
domain1.com/
whatever
domain2.net/
whatever
.htaccess
You should then be able to have normal .htaccess directives in each subdir, if you wish. Ensure you use RewriteBase / in those dirs.
Re-edit That just makes your situation more like most others'.
RewriteEngine on
RewriteBase /
# capture the original request so you never have trouble with (un)escaping
RewriteCond %{THE_REQUEST} \S+\s+(\S*)
RewriteRule ^ - [E=REQ:%1]
# ensure the domain goes to the required dir
# get domain
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)
RewriteRule ^ - [E=DOM:%1]
# explicitly set dir per host
RewriteCond %{ENV:DOM} =sub.domain1.com [NC]
RewriteRule !^sub/ sub%{ENV:REQ} [NE,DPI,L]
RewriteCond %{ENV:DOM} =domain2.com [NC]
RewriteRule !^d2/ d2%{ENV:REQ} [NE,DPI,L]
# allow domain1.com to proceed to root (any other rules go below)
# rules must still exclude subdirectories for other domains, e.g.:
RewriteRule ^(?!sub/|d2/)([^/]+)/([^/.]+)$ foo.php?bar=$1&baz=$2 [NE,B,L,DPI]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(?:sub/|d2/|index\.php$) index.php [L,DPI]
# after all other rules, emulate DirectorySlash so that Apache does not naively insert hidden directory into public URL
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (?>.*)(?<!/) %{ENV:REQ}/ [L,DPI,R]
If you're unsure of any regex, paste it into regex101.com (with the g flag off) and look at the explanation.

Why is "www.example.com" not the same as "example.com"?

I have a website. If I login in the domain of this format http://example.com and then change my address to http://www.example.com, i find my account is not logged in. If I change the address to http://example.com, I find my account is logged in.
I contacted my host, they told me its a programming issue.
How can i solve this issue so both addresses represent same access/session/cookies?
I'm using PHP & MySQL
www.example.com and example.com are two different domains as far as the browser is concerned, apparently, even though they both direct to the same site. Same would happen if you parked a different domain there, say, example.net.
In order to solve the issue, it is rather common to rewrite the URL via .htaccess. Decide upon which domain name you prefer to use and add something like this to your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
or
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301]
(the first one removes, the second one adds the www)
How can i solve this issue so both addresses represent same
access/session/cookies?
You have to set the domain path of your cookie like this to make it available on all subdomains: (www is a subdomain):
.domain.com
It is not same..
usually you can go to www.example.com just with writing example.com to your browser, but your browser added www to your url..
so basicly it is not same

Apache - Rewrite the subdomain to a location?

I am sorry, I have tried for hours to get this working, but I haven't made progress...
I want to make it so that if a user on my site types user.site.com they will be taken to site.com/user, but the URL will still show user.site.com. How can I do this? With .htaccess? Server files?
Almost there Ken
RewriteEngine On
RewriteCond %{HTTP_HOST} !www.site.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).site.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
%1 = what's before .site.com
$1 = what you got after the /
If you have test.site.com/foo.php , you would have /test/foo.php.
if you just want test, just forget about the $1.
QSA = query string append,
L = Last.
You should read the url about mod_rewrite in #phihag post.
Use mod_rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.site.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).site.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
If you want to link to resources, either use full (http://site.com/user/static/x.css) or relative (static/x.css) URLs. Absolute URLs (/user/static/x.css) will need to be crafted differently when this rule is in effect.
To keep the original address in the address bar you will need a reverse proxy rather than a redirect. Redirecting tells the browser to send a second request to the server with a different address, reverse proxy tells the server to find another page and send it without notifying the browser about it (this is what you want I believe). Reverse proxy is achieved with the [P] flag in mod_rewite
Make sure mod_rewrite, mod_proxy and mod_proxy_http are loaded and put the directives
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
RewriteEngine on
RewriteRule ^/(.*) http://site.com/user/$1 [PL]
into your virtual host configuration for user.site.com or .htaccess if you do not have root privileges. This will proxy all pages from the subdomain to the main domain folder. If you only want to proxy the index page use RewriteRule ^/ http://site.com/user instead.
I assume you are using http and not https. If so, it gets a little more complex...

Www and non www sites

I have a domain say http://www.testexample.com. When I login to http://www.testexample.com and come back to http://testexample.com in browser; the logged in user information is not displayed.
I know that the both of the above are treated differently and hence it is not retaining the session for http://www.testexample.com while accessing http://testexample.com.
Please let me know if cakephp has a way to do a match on the TLD. So whenever I type http://testexample.com it should take session for http://www.testexample.com
I am using the following code to redirect from one URL to the other
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ps6309 [NC]
RewriteRule ps6309.domain.co.in [L,R=301]
this is on my local test machine. This works sometimes and sometimes doesn't.
Also I have added the rewritelog directive to my httpd.conf file.
But the log file is not getting updated.
Please let me know if anyone has any pointers to this.
Use .htaccess to redirect all http://domain.com -> http://www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
Set the domain for the cookie as testexample.com, then it can be shared across sub domains as well as not worrying about www.
If you have many projects and don't want to hard code your domain name on .htaccess again and over again, try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
this will redirect non-www to www. While you're using cakephp, then put it on your .htaccess which is located at /webroot
Try ini_set('session.cookie_domain', $domain); (documented as ini_set session.cookie_domain and session_set_cookie_params()), where $domain is your domain name prefixed by a .. So, using the domain example.com (per rfc 2606), you'd use:
ini_set('session.cookie_domain', '.example.com');
Please note that this is not a CakePHP specific solution - looking at the code for CakeSession, session.cookie_domain is never set, meaning that it falls to it's default value. Stuffing that line in your app/config/bootstrap.php or app/config/core.php should do it for you.

Categories