I reconfigured a wildcard subdomain *.example.com and now I can access anysubdomain.example.com and always I hit the same codeingiter installation.
I am trying to load a different content when different subdomain are requested, but I need to call always the same method and class but the parameter needs to change based on the sub domain name.
Examples:
When I hit john.example.com I want to be able to see the same content I would see if I go to example.com/users/profile/john.
When I hit mary.example.com I want to be able to see the same content I would see if I go to example.com/users/profile/mary.
UPDATE:
I am using this code, but is redirecting instead of showing the content keeping the URL as a subdomain:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/users/profile/%1/$1 [QSA,L]
UPDATE 2:
Now I changed my .htaccess file to
RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ index.php?/users/profile/%1/$1 [L,QSA]
And when I access to username.example.com or when I access to example.com/users/profile/username (in both cases) and I use var_export on $_GET, it shows:
array (
'/users/profile/username/' => '',
)
But for some unknown reason, when I access to example.com/users/profile/username I see the users profile (right) but when I access to username.example.com I see the index page (I was expecting to see the users profile too).
Note: My question is not same as This question because this one is about redirecting to controllers with the name of the subdomain (which can be manually managed because controllers are limited), my question is about showing the content without redirecting (rewrite rule) of specific controller and method with a parameter equal to the subdomain (and parameters can't be manually managed because there's no a limit of values for them).
Related
I'm facing a htaccess poroblem which I can't figure it out by myself.
Let's say I want to host two websites under the same hosting plan. The main website is a Prestashop eCommerce website. The other is a placeholder for another domain name.
For example, there is the main www.myshopdomain.com in the root and www.myutilitydomain.com in a directory called Utility.
Currently, all traffic to www.myutilitydomain.com is redirected to www.myshopdomain.com. What should I do to redirect all traffic for www.myutilitydomain.com to the Utility directory, preferably without any reference to www.myshopdomain.com in the redirected URL? Is it even possible? I know I can create a subdomain, but I need the myutility domain to be accessible directly.
I tried a few approaches and managed not to break the eCommerce site, but for www.myutilitydomain.com I always get Internal Server Error.
My final attempt was this:
RewriteCond %{HTTP_HOST} ^www.myutilitydomain.com
RewriteCond %{REQUEST_URI} !^www.myutilitydomain.com$
RewriteRule ^(.*)$ /Utility/
Your RewriteCondition is not right.
You can only match against URL path in a %{REQUEST_URI} RewriteCond not the host header.
Change it to
RewriteCond %{REQUEST_URI} !^/Utility [NC]
We tried various htaccess rewrite rules but could not get to work what we need to do. Maybe some advise here?
Assume we have a main domain (a.com) with 2 add-on domains (b.com and c.com), all pointing to the website root.
Then we have folders, all in root, like /folder_a1, /folder_a2, /folder_a3,/folder_b1,/folder_b2,/folder_b3 and /folder_c1, /folder_c2,/folder_c3 in which there are php files.
Users shall be able to come to the site via:
URL=a.com/folder_1/xxx.php and be redirected to root/folder_a1
URL=b.com/folder_1/xxx.php and be redirected to root/folder_b1
URL=c.com/folder_1/xxx.php and be redirected to root/folder_c1
Each time we want to keep in the browser address bar the URL the user came from (if he came via a.com we want to keep showing a.com... etc.)
In this example we basically have to map the url string .../folder_1, dependent on the URL used, either to folder_a1, folder_b1 or folder_c1.
We tried (amongst others):
RewriteCond %{HTTP_HOST} ^www\.a\.com [NC]
RewriteRule ^folder1(/.*|)$ /folder_a1$1 [L,NC]
RewriteCond %{HTTP_HOST} ^www\.b\.com [NC]
RewriteRule ^folder1(/.*|)$ /folder_b1$1 [L,NC]
RewriteCond %{HTTP_HOST} ^www\.c\.com [NC]
RewriteRule ^folder1(/.*|)$ /folder_c1$1 [L,NC]
But that does not do the trick. With these rules we always and up at folder_a1.
Any suggestions how we can do this?
We found the solution (thanks to the support of our host rochenhost !).
As there was no answer here (yet ;), here is what we found:
The only issue was that we did not repeat the re-write condition before every re-write-rule, which meant that the condition was only applied once, to the first rule, and to none of the following rules.
Once added, all works.
Cheers
Upon request I have reformulated my question for clarity so others might benefit better from the answers.
I have 3 domains for the company I work for:
bizwizprint.com (the main website that is hosted on a shared server)
bizwizsigns.com (secondary domain with no hosting attached)
boatwiz.com (tertiary domain with no hosting attached)
The goal is to get my second and third domains to redirect to the first domain onto their own respective landing pages.
First Step: At the domain registrar, change the DNS "A Records" of the second and third domains to resolve to the same IP address that the main website is hosted on.
Second Step: Create a "Site Alias" on the main website server for the second and third domains, they will point to the root directory where the main website files reside.
Third Step: Create or edit an .htaccess file that will redirect the requests for the second and third domains and point them to the landing pages that I have created for them.
The question: What rules do I add to htaccess?
Essentially, I would like to have a user type in "boatwiz.com" in the address bar and the browser will literally GO TO "bizwizprint.com/boatwiz.html".
Please note: I do not want any rewrite rules that will change the actual URL to boatwiz.
The reason for this is that it is a temporary thing. Eventually there will be an actual "boatwiz" website and "bizwizsigns" website and they will most likely be very different in structure. I don't want it to appear that I have three domains with all the same content, or have people make any bookmarks that I will need to redirect yet again in the future.
"How do I redirect an external domain (boatwiz.com) to land in a specific page of a new domain (bizwizprint.com/boatwiz.html) without any rewriting?"
So you probably mean that you want an "internal redirect", not the "external redirect", right? I.e. you want e.g. the bizwizsigns.com to stay displayed in the browser Location bar, but show the contents
of bizwizprint.com/signs, right?
1. htaccess only - impossible
Well, using only .htaccess, this is impossible, because the different domain will force the external redirect. Citing the docs:
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and
hostname are stripped out and the resulting path is treated as a
URL-path. Otherwise, an external redirect is performed for the given
URL. To force an external redirect back to the current host, see the
[R] flag below.
2. Iframe trick
What you could do is to use iframe. Put this code at bizwizsigns.com/index.html:
<iframe src="http://bizwizprint.com/signs" width="100%" height="100%"
style="border: 0 none;" frameborder="0">
But there are many downsides of this solution:
the URL will not change in the browser's location bar, even if user clicks within the iframe
browser bookmarks and history will not work as expected.
3. Clever hosting setup (domain aliases into the same dir)
Are you in a hosting environment, or do you have your own server? Sometimes the hosting allows you to make aliases of several domains that are handled by the same local directory tree. In that case, you won - you can write .htaccess so that it handles the requests as internal redirects:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ /signs [L]
RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ /boats [L]
which will (internally) redirect bizwizsigns.com to /signs (= your content of bizwizprint.com/signs, because you have one hosting server directory for all 3 domains). But if you e.g. want all queries like bizwizsigns.com/<foo> to be redirected to bizwizprint.com/signs/<foo>, you have to be more careful - see the added condition on REQUEST_URI to prevent endless loop:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/signs/ [NC]
RewriteRule ^.*$ /signs/$1 [L]
Assuming that you have all 3 domains pointing to the same document root, you just need this in its htaccess file:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/signs [L,R=301]
RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/boats [L,R=301]
RewriteCond %{HTTP_HOST} (bizwizsigns|boatwiz)\.com$ [NC]
RewriteRule ^(.+)$ http://bizwizprint.com/$1 [L,R=301]
So if it's just http://bizwizsigns.com/ or http://boatwiz.com/, then you get redirected to http://bizwizprint.com/signs or http://bizwizprint.com/boats. But if you have anything after the last /, like http://bizwizsigns.com/foo/bar.html then you'll get redirected to http://bizwizprint.com/foo/bar.html.
Here is what I'm trying to do.
I have 2 apps running on my site one at root / and one at at /blog/.
I want to be able to access some pages that are served by the blog app but without the /blog/ part in the URL. This bit I have managed to do with the following rule.
RewriteRule my-page blog/index\.php?page_id=1 [L]
This allows me to view a page actually at /blog/my-page at /my-page.
Now where I'm struggling is with ensuring there are no duplicate URLs, so I'm trying to redirect to my shorter URL. Like so:
From:
/blog/my-page
To:
/my-page
From:
/blog/index.php?page_id=1
To:
/my-page
With the following rule, I can redirect from /index.php?page_id=1 to /my-page
RewriteCond %{QUERY_STRING} ^page_id=1$
RewriteRule index\.php$ my-page? [R=301,L]
After this rule happens the first rule I mentioned takes it to the right place.
My question is:
How can I get it to work with blog as part of the URL. I expected the below to work but it doesn't
#RewriteCond %{QUERY_STRING} ^page_id=1$
RewriteRule ^blog/index\.php$ my-page? [R=301,L]
The reason this was not working was because there was an .htaccess file within my /blog/ directory.
This included a rule that would catch anything and route to blog/index.php.
Moving my rules to /blog/.htaccess allowed them to function properly
I have a client that has a good amount of domain alias' and wants them all redirected to the one main domain on the site. They also want to know which of the domain alias' is doing the redirecting. I have that part down but I want to optimize the code to the best most proper way it should be and to eliminate the amount of code I have to write. I am wanting to know if there is a way to pass to the RewriteRule url the domain alias that was used.
This is what I have now. I am looking for the domain alias that is being hit and then passing that alias to the url. Then in google analytics I can see how many times that url was used to hit the page.
RewriteCond %{HTTP_HOST} ^(www\.)?domain-alias1\.com [NC]
RewriteRule ^(.*) http://www.main-domain.com/?domain-alias1\.com$1 [R=301,L}
But my goal is to not have to write both the condition and rule for every single domain alias.
Is there a way to see which alias was hit and then have the rewrite rule automatically add that to the position I have specified?
I had originally tried something like this just to see if it would work(although I have tried many different ways):
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z]+)\.com [NC]
RewriteRule ^(.*) http://www.main-domain.com/?$1\.com$2 [R=301,L]
You can try something along these lines:
RewriteCond %{HTTP_HOST} !^(www\.)?main-domain\.com$ [NC]
RewriteRule ^(.*) http://www.main-domain.com/$1?domain=%{HTTP_HOST} [R=301,L]
With this any request NOT for domain www.main-domain.com will be redirected to www.main-domain.com with the domain name in query string domain.