I have a site with virtual pages where any request is rewrote to the index page in this way:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
I.e, http://www.example.com/job/edit is rewrote as http://www.example.com/index.php/job/edit where I parse it. That works great.
Now, I need to process every virtual subdomain in order that i.e. http://user1.example.com/job/edit becomes http://www.example.com/index.php/user1/job/edit.
I added in CPanel a wildcard subdomain (*.example.com), so every subdomain is pointed to the domain folder (without it I got 'Server not found' on every subdomain).
I tried every example that I found here, but I can't get them to work. If I print out $_SERVER['REQUEST_URI'] on the index page, I allways get the last part, but not the subdomain (i.e. /job/edit).
Is there any way of doing this?
Thanks in advance.
Edit: I already have a workaround for doing it. The $_SERVER['HTTP_HOST'] var contains the whole domain, i.e. user1.example.com. I can parse it in PHP and extract the subdomain, but I think it should be a more elegant way of doing it.
I have Done Same Like this.
Create a Folder called "user"
Create A index.php to handle The calls To user functions
Add A Virtual host in the Httpd.conf That Route The
*.example.com to the Path /public_html/user
<VirtualHost 0.0.0.0>
ServerAlias *.example.com
ServerAdmin webmaster#yourdomain.com
DocumentRoot /home/yourdoma/public_html/user
ServerName yourdomain.com
User yourdoma
Group yourdoma
BytesLog /usr/local/apache/domlogs/yourdomain.com-bytes_log
CustomLog /usr/local/apache/domlogs/yourdomain.com combined
ScriptAlias /cgi-bin/ /home/yourdoma/public_html/joe/cgi-bin/
</VirtualHost>
Now all Call will route to user Folder.
Add a Htaccess file in user folder to rewrite the base path
Related
few hours later, i need help. I trie to rewrite the url for using wildcard subdomains.
My domains will be *.domain.com - for example mytest.domain.com
For every subdomain is a subfolder unter www.domain.com/sdom/content/mytest
I've setup the apache/nginx server. For testing i upload a index.html inside the "content" folder.
If i type mytest.comain.com this index.html will display. So now i upload the .htaccess file inside this folder to route from here to the destinations (in this example to the "mytest" folder).
I trie a lot of thiks, at least this
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule (.*) /httpdocs/domain/sdom/content/$1/index.html
i call mytest.domain.com but it wont go to the subfolder, it go to the maindomain.
If i delete the htaccess file, the index.html of the "content" folder will display.
After rewriting the url in the adressfield of the browser shoul display mytest.domain.com - not the new path to the subfolder etc.
What i have to do, that is works ?
UPDATE
subdomains route to -> mainfolder
subdomains should route by htaccess to folder that has the same name of the subdomain
So i trie htacess
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld$
RewriteRule ^$ /subdomainfolder/index.html
With .htaccess files you can't. Here mytest.domain.com and www.domain.com are different hostnames. So inevitably an external redirect will happen.
If you want different domains run by one physical server you should use virtual hosts:
<VirtualHost *:80> # Change this to *:443 if you use SSL/TLS
ServerName mytest.domain.com # Your domain name
DocumentRoot $SRV_ROOT/httpdocs/domain/sdom/content/mytest/ # assuming $SRV_ROOT is set to root of your server
<Directory "$SRV_ROOT/httpdocs/domain/sdom/content/mytest/">
Require all granted # on Apache 2.4
# Below are instructions for Apache 2.2
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
You'll need to restart your server each time you add a subdomain, as well as having a DNS record or local record in /etc/hosts file if you test locally.
I think this is an easy one, but i'm really confused myself.
myproject.com/index.php shows my project main page
myproject.com shows my project main page too.
In 1st case, after routing it works fine (i.e. : myproject.com/index.php/register)
In 2nd case, after routing it fails (i.e. : myproject.com/register) with following error : The requested URL /register was not found on this server.
So because of that, i thought that i had to re-route every request for myproject.com to myproject.com/index.php
app/config/app.php :
'url' => 'http://127.0.0.1/public',
etc/httpd/conf/httpd.conf :
<VirtualHost *:80>
DocumentRoot "/var/www/html/myproject/public/"
ServerName myproject.com
</VirtualHost>
Nothing's been configured in hosts file.
Goal: how can i re-route all requests from myproject.com to myproject.com/index.php with hiding index.php part of from users ?
Visitors should see that : myproject.com
but i want them to actually reach : myproject.com/index.php
Workaround:
<VirtualHost *:80>
DocumentRoot "/var/www/html/myproject/public/index.php"
ServerName myproject.com
</VirtualHost>
Result : I cannot reach the assets, (js, css files) results with 404 because my browser is not allowed to reach myproject.com/public/. Root has been set as myproject.com/public/index.php in this virtualhost settings.
Check if the .htaccess file is present in your public directory.
If not check the Pretty Url Section.
Also make sure that you put <Directory> directives in your vhost configuration just to be on the safe side.
Eg:
<Directory "/var/www/html/myproject/public/">
AllowOverride All
Allow from All
</Directory>
Concerning the 'url' => 'http://127.0.0.1/public', in app.php am pretty much sure the /public is kinda extra.
'url' => 'http://127.0.0.1', should be working fine.
You need rewrite rules for this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Laravel docs: http://laravel.com/docs/5.1#pretty-urls
i have a domain eg. example.com
and it contains two folder
/cakeapp1
/cakeapp2
if i use individualy then it is working fine.
eg. http://example.com/cakeapp1 or http://example.com/cakeapp2
But i want these to point on the main domain some links are from first sub-directory and other are from second sub-directory.
Basically i want to do the following.
If i open http://example.com then it point to http://example.com/cakeapp1
if i open http://example.com/file then it point to http://example.com/cakeapp2/file
and
if i open http://example.com/random then it point to http://example.com/cakeapp1/random
Please suggest me the way that how i can do the above task.
There is problem with the htaccess also. if i use the htaccess in root and use rewrite then the both subdirectory's htaccess conflict.
Thanks for help!
In the httpd-vhosts conf file add a virtual host entry. Try below (i havent tested it but it might give you an idea)
<VirtualHost *:80>
ServerName example.com
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/file
RewriteRule ^/(.*) http://example.com/cakeapp2/file [R=301,L]
RewriteRule ^(.*) http://example.com/cakeapp1$1 [R=301,L]
</VirtualHost>
I've been searching similar solutions but none of them seems working on my server.
What I'm trying to do is set redirect from yyy.zzz.com (subdomain) to zzz.com (primary domain) without rewriting url. So both yyy.zzz.com and zzz.com actually pointing at same directory and same files in it.
Currently I have this .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^yyy\.zzz\.com$ [NC]
RewriteRule ^(.*)$ http://zzz.com [L,NC]
Which, of course, just redirects straight on, and url actually changes.
Just for information, I want to set such subdomain url for CMS, so if user wants to enter CMS he does it from subdomain, while actually only $_SERVER['HTTP_HOST'] is changing.
Any help would be appreciated.
You should set up a ServerAlias in your apache config. Setting yyy.zzz.com as an alias for zzz.com
<VirtualHost *>
ServerName zzz.com
ServerAlias yyy.zzz.com
# ...
</VirtualHost>
http://httpd.apache.org/docs/2.2/mod/core.html#serveralias
On my website i'm using virtual host so my users can have virtual domain like 'user1.mydomain.com', 'user2.mydomain.com',...
The problem is that on virtual domains like 'user1.domain.com' the index page is always the same as on my index page 'http://mydomain.com'.
What i want to do is to have two different index pages for the domain and for subdomains. My question, how to have subdomains redirected to 'index2.php' (for example) and still have the subdomains to look like 'user1.mydomain.com'?
Ideally, you would set up completely separate document directories for each subdomain. vhost in apache is the way to go. If you however want to do it your way (subdomains redirecting to individual files), then it's a bit more work, but is still doable. First, define the mod_vhost with wildcard:
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.mydomain.com
ServerAlias *.mydomain.com
...
</VirtualHost>
Then inside this VirtualHost setup rewrite rules using mod_rewrite:
<Location "/">
RewriteCond %{HTTP_HOST} ^user1.mydomain.com$
RewriteRule ^\/$ http://www.mydomain.com/index2.php [R=301,L]
RewriteRule ^\/index.php$ http://www.mydomain.com/index2.php [R=301,L]
RewriteCond %{HTTP_HOST} ^user2.mydomain.com$
RewriteRule ^\/$ http://www.mydomain.com/index3.php [R=301,L]
RewriteRule ^\/index.php$ http://www.mydomain.com/index3.php [R=301,L]
...
</Location>
Note however that this will only work properly for / and /index.php requests to subdomains. You are much better off setting up separate document root directories for each subdomain if you intend to do anything more than this.
Maybe you are interested in this.
http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html#virtualdocumentroot
This allow, in Apache server, to automatically use a document root based in the subdomain
I would use php to check the url.
If it is a subdomain include index2.php else include index1.php
Without knowing the servers you are running on it is the only method I could think of.