Catching the domain name with .htaccess - php

I'm quite new in using .htaccess and modrewrite.
I have a multitenant website in php and every tenant can save a domain name he bought in the app database after pointing it to my server ip.
Now I'm trying to write a rewrite Rule via .htaccess so that when the user types his domain in the addressbar (e.g. www.example.com) the .htaccess parses the hostname and passes it as a parameter to an index page, so that every request done to www.example.com becomes something like that: /index.php?domain=www.example.com .
And even www.example.com/user to become /index.php?domain=www.example.com&url=user
How can I do it with .htaccess?
Thank you for your help !

You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} !domain= [NC]
RewriteRule ^(?:index\.php)?$ index.php?domain=%{HTTP_HOST} [L,QSA,NC]
RewriteCond %{QUERY_STRING} !domain= [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?domain=%{HTTP_HOST}&url=%{REQUEST_URI} [L,QSA]

Related

Redirect a page from one domain to another domain (both domains belong to one website)

At first glanse question is pretty common, however other answers I have seen do not work in my case.
I need to redirect a page from one domain to another domain. Both domains have one web-site under them and one common .htaccess file.
www.olddomain.com/guides -> www.newdomain.com
I use .htaccess file. If someone knows answer for other way - you are welcome also.
My current file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
I have tried this way:
RewriteEngine on
Redirect 301 http://www.olddomain.com/guides https://www.newdomain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
Also I tried this way:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^http://www.olddomain.com/guides$ https://www.newdomain.com [R=301]
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
Does not work. How it can be done properly?
I believe your Redirect 301 line is the way to go, but should read something like this:
Redirect 301 /guides https://www.newdomain.com
See here for more information.
Edit to add:
The above will redirect to https://www.newdomain.com/
If you want a certain path, then just put
Redirect 301 /guides https://www.newdomain.com/foo
where foo is the path on the server you want to end up at.
I end up with the following.
Unlinked "olddomain" name from the origin site.
Created new website folder at my hosting with only one file .htaccess in a root that contains only one line as #CBaish suggested:
Redirect 301 /guides https://www.newdomain.com
Linked "olddomain" to this folder
Now all traffic from the page "/guides" of olddomain will go to my newdomain.

htaccess routing with url parameters

enter code here Hi I've the following code inside my htaccess file.
My wildcard subdomain routes to "mainfolder" - here i placed the htaccess file.
I've the following folders
"mainfolder"
"mainfolder/sub1"
"mainfolder/sub2"
etc.
Calling the subdomain - sub1.domain.com it should route to the subfolder "sub1" (subfolder=subdomain).
I tried to do it with this code
#Redirect to subdomainfolder if no special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^$ %1/index.html [L]
#Redirec to subdomainfolder if a special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain.com(.*)$ [NC]
RewriteRule ^(.*) %1/$1 [L]
The first rule works well, but if I add the second rule I receive a internal server error.
Whats wrong with this - how how I can change the first rule in this way, that it works with all url-parameters after .com - that was the reasons for me to add the second rule.
Hope I get help for this. thanks a lot.
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).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/subfolder/(.*)$ http://subdomain.example.com/$1
LIke
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^sub1/(.*)$ http://sub1.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^sub1\.example\.com$
RewriteCond %{REQUEST_URI} !^sub1/
RewriteRule ^(.*)$ /sub1/$1 [L,QSA]
If more understanding step wise then follow https://beginnersbook.com/2013/08/redirecting-from-subdirectory-to-subdomain-using-htaccess/

.htaccess - Redirect subdomain to folder

This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.
I simply want m.daltonempire.nl to be redirected to daltonempire.nl/m/ without the user seeing the URL change.
So if m.daltonempire.nl/hello.php is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php.
Note: I do not want www., so simply http://m.daltonempire.nl
Thanks in advance,
Isaiah v. Hunen
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]
The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.
Try this example:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]
Any requests http://test.example.com will be mapped to http://example.com/test/...
Try googling dynamic subdomain with php and htaccess to get better search results.
I have set CNAME of my sub domain below:
blog.mydomain.com
to my wordpress that installed in folder /blog/ under root directory.
Formerly I need to use this url to call wordpress:
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
Finally I got .htaccess setting that is work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
I have also CNAME other subdomain: http://forum.mydomain.com to mybb installation in folder /forum/mybb/ so the .htaccess need to put [L] on each of RewriteRule code as below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/$1 [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.

Pointing the website http root to a sub folder with htaccess?

How can I use RewriteRule to point the website http root to a sub folder?
I always use the rule below but now after upgrading to the latest wamp server, it does not working anymore. Why?
RewriteRule ^/?$ local/applications/bin/oldsite/index.htm [L,QSA]
I want to point, for instance,
http://mysite.com/ to http://mysite.com/local/applications/bin/oldsite/index.htm
or in localhost wamp server, it should be like this,
http://localhost/mysite/ to http://localhost/mysite/local/applications/bin/oldsite/index.htm
Any ideas?
And also,
http://localhost/mysite/contact.htm to
http://localhost/mysite/local/applications/bin/oldsite/contact.htm
http://localhost/mysite/about.htm to
http://localhost/mysite/local/applications/bin/oldsite/about.htm
EDIT
I managed to point the other pages to the specific locations with the rule below,
RewriteRule ^([a-zA-Z0-9\-]+)\.htm/?$ local/applications/bin/oldsite/$1.htm [L,QSA]
But still I cannot point
http://localhost/mysite/ to http://localhost/mysite/local/applications/bin/oldsite/index.htm
UPDATED
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#### localhost sets of rules
# No target file, go to index.php
RewriteCond %{HTTP_HOST} localhost [NC]
RewriteCond %{REQUEST_URI} ^/mysite/?$
RewriteCond %{REQUEST_URI} !index\.htm [NC]
RewriteRule . mysite/local/applications/bin/oldsite/index.htm [R=301,L]
# Target file, go to file
RewriteCond %{HTTP_HOST} localhost [NC]
RewriteCond %{REQUEST_URI} ^/mysite/([^\.]+)\.htm [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . mysite/local/applications/bin/oldsite/%1.htm [R=301,L]
#### Live site set of rules
RewriteCond %{HTTP_HOST} (?:www\.)?mysite\.com [NC]
RewriteCond %{REQUEST_URI} !index\.htm [NC]
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule . local/applications/bin/oldsite/index.htm [R=301,L]
The first sets of rules are for the local host and redirect permanently:
http://localhost/mysite/filename.htm
To:
http://localhost/mysite/local/applications/bin/oldsite/filename.htm
When filename.htm is not present in the incoming URL, filename.htm in the substitution URL is always index.htm
The string mysite is assumed to be a fixed string while filename is assumed to be a variable.
The second rule-set is for the web server and redirects permanently only
http://mysite.com/
To:
http://mysite.com/local/applications/bin/oldsite/index.htm
In the last case, if the incoming URL holds any additional path the rule is not applied.
For silent mapping, remove R=301 from [R=301,L].
Copy the above sets of rules into both .htaccess files in the local and remote root directories.
This might get you started (untested):
Check in a condition whether the request does not point to a particular subfolder. If so, redirect the request to a subfolder. You can use in the target if you want. Instead of the HTTP Status 301 you can use another one if more appropriate.
RewriteCond %{REQUEST_URI} !^/(local/applications/bin/oldsite/.*)$
RewriteRule ^(.*)$ http....n/subfolder/%1 [R=301,L]
It seems like you want to make the "oldsite" directory your Document Root - right?

Prevent url with a directory from pointing to a web page

I have a domain let's call it newsite.com which is parked to maindomain.com. I use mod-rewrite to direct all requests for newsite.com to a subdirectory on maindomain.com. On newsite.com I have CodeIgniter installed. Which means the urls look like this:
newsite.com/products/shoes/
The above works just fine.
However, if I set up a controller for a page like this:
newsite.com/about/ or newsite.com/about/faqs/
I get a 404 which I believe is being caused because it is looking for the page maindomain.com/about.php which does exist, but does NOT exist on newsite.com.
My question is.... how do I PREVENT urls like newsite.com/about/ from pointing to newsite.com/about.php ? This is the opposite of what many people try to do (they tend to want to allow the file extension to be missing).
I'm wondering if it is and apache or PHP setting that causes it to look for the file first if it exists and the directory does not? Or do I need to add more mod-rewrite rules?
Thanks very much!
Edit - here is my .htaccess file currently. It resides at the web root of the maindomain.com shared hosting account.
# Redirect various urls to subfolders of the format: /site-thename/
RewriteEngine On
# Remove the www for NewSite.
RewriteCond %{HTTP_HOST} ^www.newsite.com$ [NC]
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]
# Handle pages and directories that should not go through CodeIgniter.
RewriteCond %{HTTP_HOST} ^(www\.)?newsite\.com
RewriteCond %{REQUEST_URI} !^/site-newsite/
RewriteCond %{REQUEST_URI} ^/demo/ [OR]
RewriteCond %{REQUEST_URI} ^/robots\.txt [OR]
RewriteCond %{REQUEST_URI} ^/emailform\.php [OR]
RewriteCond %{REQUEST_URI} ^/assets/ [OR]
RewriteCond %{REQUEST_URI} ^/index\.php
Rewriterule ^(.*)$ /site-newsite/$1 [L]
# Handle CodeIgniter URLs.
RewriteCond %{HTTP_HOST} ^(www\.)?newsite\.com
RewriteCond %{REQUEST_URI} !^/site-newsite/
Rewriterule ^(.*)$ /site-newsite/index.php?/$1 [L]
Asked my hosting company's support department what might be causing this... and it turns out that it is caused by Apache MultiViews. This can be turned off via
Options -MultiViews
in the htaccess file. That worked.
Thanks for the suggestions.
If you have no rewrite rules for it, Apache, by default, will reference whatever is on the filesystem. You must have rewrite rules there to tell Apache to use a specific controller to handle requests instead of attempting to reference objects on the filesystem.
I assume that if you are an apache user, you use .htaccess file for redirecting the URL to you 'index' file (let's call it index.php). Also you should have sth like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
in your .htaccess file. Note that the RewriteCond condition has a parameter which checks if it is not a file:-) More can be found at http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

Categories