Redirect without changing Url htaccess - php

I'm wondering if this is possible - I have projects structured like:
www.mydomain.com points to MyFolder on server. In MyFolder i have 3 App Folders eg MyFolder/App1, MyFolder/App2..
Each of these are 3 different web applications. Url of domain is www.mydomain.com
Is it possible, that, when url user go to mydomain.com, it automatically redirect to mydomain.com/App1 but without url change visible to user (in browser it will still be mydomain.com)?
I can not point domain to MyFolder/App1 because that way app2 is not accessible.
My solution is that app1 becomes a root which contains app2,app3 etc folder but thats just to messy
edit: question clarification

You should be able to use this code here.
RewriteEngine on
#ignore App1 and App2 and interally Rewrite to /App
RewriteCond %{REQUEST_URI} !(App1|App2) [NC]
RewriteRule ^(.*)$ /App/$1 [L]

Yes, you should be able to do it in your .htaccess file like this (untested code):
RewriteCond %{HTTP_HOST} ^example.com/App
RewriteRule ^(.*) http://www.example.com/App1 [P]

Related

How can I Alias Or Rewrite the subfolder to a domain

I have a folder structure like this
/
/project1
I normally use it with one domain (example.com), but now I want to use it as a sepreate domain (project1.example.com) and I am using apache virtualhost.
I have succeed to setup, but some problem on the link happened.
All the hyperlink in project1 is the path included the "project1" subfolder (e.g. project1/image/pic.jpg).
I setup like this
DocumentRoot /doc_root/
Alias /project1 /doc_root/project1/
Alias / /doc_root/project1
RewriteRule /project1/(.*) /$1 [R]
The problem is when i rewrite clean url
e.g. example.com/project1/post?id=1 => project1.example.com/post/1
With
RewriteRule ^/post/(\d+)$ /project1/post.php?id=$1 [PT,NC,L,QSA]
I am getting 404 error in the ajax post in the page
Since the ajax url is pointed to /project1/process.php
And I think this request is not rewriting.
What is the proper way to setup like this? thanks!
There's several issues here:
The Alias rules don't look correct and are confusing.
The redirect rule you provided won't extract a query string parameter (i.e. it won't rewrite the path from /project1/post?id=1 to /post/1) nor does it alter the domain name (i.e. it won't change the domain to project1.example.com).
There's no need for the redirect.
I think what you want is something like:
DocumentRoot /doc_root/
RewriteCond %{HTTP_HOST} ^project1\.example\.com$
RewriteRule ^/(.*)$ /project1/$1 [NC,PT]
If you do actually want to redirect, consider:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/project1/(.*)$ project1.example.com/$1 [NC,R]

.htaccess 2 domains, same target, 1 redirect

I own 2 domains that are pointing to the same static ip. I wanted to set domain1.com to be redirected to domain1.com/down.php but only for the target domain1.com and domain.com/index.php/html.
The domain2.com should work as usual, so no redirects here. It should target /var/www/html like it currently does.
Background is: that there was a project which should not be available on the domain1.com, but there are specific files such as domain1.com/subdir/subdir/file1.jpg, which should still be accessible there.
Any ideas?
Many Thanks
.htaccess to redirect domain1.com/*
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ down.php?_url=/$1 [QSA,L]
The rule only works if the condition is meet. ?_url=/$1 is not needed but may prove useful.
If you use htAccess, also it seems you use Apache2
You can also use virtual host directly from apache 2 configuration :
https://httpd.apache.org/docs/current/vhosts/examples.html

htaccess mod_rewrite redirect to directory without displaying on url

I have a site called www.example.com and I have my php files in it. I store all my working files in www.example.com/site. I want to view the site in www.example.com instead, without moving my site content. What can I do?
This is currently what I am typing in .htaccess. It will redirect my site to www.example.com/site but I think the url is ugly
RewriteEngine on
RewriteBase /
RewriteRule ^(/.*|)$ /magento$1 [NC,L]
Let me see if i understood this correctly.
You have a domanin, www.example.com, and on this domain you want to display the content of a directory, www.example.com/site !?
If this is the case then you need to change the document root
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/site
RewriteRule ^(.*)$ /site/$1 [L]
or
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /site/$1 [L,NC]
There are 2 things you must consider :
If I understand your request, you are accessing some PHP files using www.example.com, but what you want is to access www.example.com/site, but without the /site, right ?
So basicaly, what you're looking for is NOT rewrite, it's just pointing your domain to the good folder, which is /site, right ?
If you're using Apache2, you have to edit your apache's configuration file in /etc/apache2/site-available/default (or remplace default with the name of the virtualhost you may have created).
In this file, look for the directive DocumentRoot. It should lead to the "root" directory of your pages (the one you access typing www.example.com)
I think you just have to append /site to this DocumentRoot and then reload your apache2 with service apache2 reload
You're website www.example.com will now lead to the correct directory.
If it's still not working, you must consider looking into magento's admin, because magento is rewriting url according to the Base URL you specify inside Admin / Configuration / general / web.
You'll have to modify Base_URL in both Secure and Un-Secure sections.
Then it should work fine.
I would comment out the Rewrite bloc you're using at the moment, or maybe I didn't fully undestand what you want to achieve.

Sub Domain Name Creation in .htaccess

I have an application on SAAS basis where I will be adding clients from backend or the clients can even create their own panel by signing up
While signing up or adding the client , the client or I will be assigning the sub domain . So suppose I am assigning subdomain xyz to client1 . Then the access url will be
xyz.maindomain.com
Now , I want .htaccess code in such a way that if xyz.maindomain.com is called, the url called must be maindomain.com?client=xyz .
Please guide me how to do this.
Thanks in advance.
We could use mod_rewrite to achieve what you wanted. No server-side scripting such as PHP required.
Say, put this in .htaccess file on your root www directory (or any relevant Apache *.conf file if you wish)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/?client=%1/$1 [L,R=301]
This will redirect foo.example.com to http://www.example.com/?client=foo
(change example.com to your domain name)
More about mod rewrite, with examples:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708
Hope this help.

Create a Subdomain in godaddy hosting using PHP

Is there any easiest way to create a sub domain using PHP code on godaddy.com hosting?
e.g:
jon.mywebsite.com
jessie.mywebsite.com
etc
you need to create a A record to serve all your subdomains
*.your-site.com IN A YOUR-IP-ADDRESS
then you need to create a .htaccess file and
RewriteCond {REQUEST_URI} !\.(png|gif|jpg)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?uri=$1&hostName=%{HTTP_HOST}
this will ignore images (SEO friendly URLs).
Now you can redirect your users to $userName.your-site.com
Alternatively try this:
setup your application so your users goes to
your-site.com/user
your .htaccess should look like this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([aA-zZ])$ index.php?username=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.mywebsite.com
RewriteRule (.*) index.php?username=%1
so now when you hit the index.php it will grab the user and redirect to $user.your-site.com as a custom subdomain. (in this case usernames are limited to a-z characters)
I dont think with PHP Code you can but with the help of .htaccess i think it is possible.
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+jon/
RewriteRule ^jon/(.*)$ http://jon.mywebsite.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+jessie/
RewriteRule ^jessie/(.*)$ http://jessie.mywebsite.com/$1 [L,R=301]
RewriteRule ^(jon|jessie)/ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?jon\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jon/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?jessie\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ /jessie/$1 [L]
You can make subdomain much easier using control panel, why to do it the hard way ?
This is an old question, but in case anyone is looking for the answer. This is specific to GoDaddy, but other Hosts may do something similar to this.
After you setup your DNS A record you need to go to Hosted Domains and add the subdomain folder name under the root domain account.
Go to Hosted Domains.
Under Hosted Domains you will see a root domain table column.
You will also see a Subdomains table column with an "Add" link.
This link is for adding subdomains to a root domain.
Type in/enter the name of the subdomain folder.
Create either an index.php or index.html file and upload it to your subdomain folder and echo something like "Setup Completed". I noticed that if you are doing something like creating a restricted "api" subdomain for example and the GD script cannot access the index file in your subdomain folder then the "process" does not complete until you add a plain index.php or index.html file in your subdomain folder. After that is done then you can add anything you want in that subdomain folder/site.

Categories