View folder instead of public_html - php

How can i "redirect" my page, to show /v4-beta/ instead of ./ when accesing my domain?
Not only redirect but show the content of v4-beta on the domain and not www.domain.com/v4-beta ? but showing www.domain.com but all content is from v4-beta ?

A simple solution would be to include the subfolder's index file in the top-level index file.
public_html/index.php:
<?php
include("v4-beta/index.php");
?>

You are looking for the rewriting module that comes with the apache http server:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/v4-beta/
RewriteRule ^(.*)$ /v4-beta/$1 [L]
The module has to be loaded first. Then you can use those commands inside the mail server configuration or inside so called .htaccess files. The frist option is the preferred one.

Htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteCond %{REQUEST_URI} !^/v4-beta/
RewriteRule (.*) /v4-beta/$1

Related

Why does WAMP run script with similar name instead of rewriting URL?

I'm trying to rewrite all requests to index.php and then there decide which file to include depending on the value in the $_GET['p'] variable. For example I have a script called update.php in the home directory of my site called leltar, which I would like to be included if the opened page is localhost/leltar/update.
However, the problem is that the rewriting does not work because WAMP runs the script even though the .php extension is not in the link. The output is just the one from the script, nothing is shown from index.php. How can I stop WAMP from running the script with similar name? I suppose, there is something wrong with my .htaccess code as well because if I open pages other than localhost/leltar/update, the value of $_GET['p'] is the string "index.php" all the time.
.htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ index.php?p=$1
index.php:
// ...
switch ($_GET['p']) {
case 'update':
require_once('update.php');
break;
default:
// something else
break;
}
// ...
The content of update.php is not relevant.
EDIT:
The main problem is that by default or even when I use the RewriteCond %{REQUEST_FILENAME} !-f rewrite condition if there is a file with the same name as the user-friendly end of the link, WAMP somehow overrides my rewrite rules. If I put a picture named pic.jpg into the www folder and open localhost/pic.jpg, the image shows up, obviously. However, if I leave out the extension visiting the page localhost/pic, I get the same output as well (instead of getting a 404 Not Found error because of the non-exisiting folder, I suppose).
EDIT 2:
On a real server there isn't any problem. If I leave out the extension a 404 error is thrown, so it's definitely a WAMP-specific thing.
Rules in .htaccess files effectively loop until the URL does not change (because the processing is restarted each time, which means the rules are also processed again) so of course your rule loops until p is equal to itself.
You don't actually need p, you can just check $_SERVER['REQUEST_URI'] in your PHP script and that will tell you the original requested URI (not the same as REQUEST_URI below, which does change with each rewrite).
So just use this:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^ index.php [L]
But... you probably want files that exist to be served, such as images, scripts etc. so the usual thing is to do this and check if the file exists:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Although it's more efficient to do this and list the directories with files in them that you want to be served:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !^/(?:images|css|scripts)/
RewriteRule ^ index.php [L]
Just change the list to the names of the directories that have your static files in.
Update
From the comments, it seems you have another rewrite that is adding .php to URLs, so they can work without it. In order to not rewrite them to index.php, I suggest putting the rewrite before this one. Otherwise, you can check if the URL exists with .php on the end of it like this. Perhaps combining it with the folder check rather than having two file-system checks.
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteCond %{REQUEST_URI} !^/(?:images|css|scripts)/
RewriteRule ^ index.php [L]
You can add specific file exceptions to the third rule like this:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !^/(?:images|css|scripts)/
RewriteCond %{REQUEST_URI} !=/some/url.php
RewriteCond %{REQUEST_URI} !=/some_other_url.php
RewriteRule ^ index.php [L]
Indeed, as #SuperDuperApps assumed, the problem was that WAMP has MultiViews enabled by default. Adding Options -MultiViews to my .htaccess file solved all my problems, my initial code mentioned in the question works fine now.

Load Multiple Urls as One Page

Not 100% sure how but I know its simple .htaccess but I have no idea what to do.
Basically I want it to load a index.php file no matter what url they go to in the folder containing the index.php for example:
http://website.com/folder/thisisntafile.php will load: index.php in the folder named folder. This will happen for whatever /folder/file.php is loaded.
Thanks!
Try this :
RewriteEngine on
#Rewrite "folder/file"
#don't rewrite "folder/index.php"
Rewritecond %{REQUEST_URI} !/index.php$
RewriteCond %{REQUEST_URI} ^/([^/]+)/[^.]+(\.php|html)?$
RewriteRule ^ /%1/index.php [NC,L]
This rewrites
http://example.com/foo/bar.php
to
http://example.com/foo/index.php

.hatccess file to redirec to subfolder/subfolder

I am nto able to rewrite URL as I need.
I have the following document structure:
1) root - root folder where .htaccess should be placed in
2) root/folder1 - subfolder
3) root/folder1/public - this is folder where index.php is
Thus I need that mydomain.com would open index.php inside "public" folder. And all other requests would go via it.
I tried this, but it doesn't work (p.s. I am writing rewrite rules first time). I put it inside root folder.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{REQUEST_URI} !^/$1
RewriteRule !^folder1/public /folder1/public%{REQUEST_URI} [L,NC]
I have a similar .htaccess like yours, so I edited a bit mine. Does this worked for you?
The condiftion for .css|.js is needed otherwise it will send as plain/text instead of normal css/js
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js)$
RewriteRule ^([^?]*)$ root/folder1/public/index.php [NC,L,QSA]

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.

Redirect domains to path on main domain

I have some domains:
http://domainmain.com
http://domainone.com
http://domaintwo.com
My secondary domains are currently hosted under the main domain. No sub-directories, no other paths. So every domain get the contents of http://domainmain.com.
For better understanding: These files points all to the same file: http://domainmain.com/index.php, http://domainone.com/index.php, http://domaintwo.com/index.php.
For every domain I have a folder located at http://domainmain.com:
domainname folder / path
-------------- -----------
domainmain.com /
domainone.com /domainone
domaintwo.com /domaintwo
My goal is to redirect every domain to the corresponding dir / path http://domainone.com.
For example: http://domainone.com has to show the content of path /domainone. The visiter has to see http://domainone.com. This also should work: http://domaintwo.com/images shows http://domainmain.com/images.
Some code I started with in the .htaccess file:
RewriteCond %{HTTP_HOST} domainone.com [NC]
RewriteCond %{REQUEST_URI} !^/domainone
RewriteRule ^(.*)$ /domainone/$1 [NC,L]
And some PHP (but I want to use redirect instead of file_get_contents():
if ($_SERVER['SERVER_NAME'] == 'domaintwo.com') {
echo file_get_contents('http://domainmain.com/domaintwo');
die();
}
Note: It is only possible to have an .htaccess file at http://domainmain.com. My server runs PHP5.
Your question is quite similar to this one asked on the web-master's section;
How to redirect different domains to separate subdirectories.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(domainone)\.com$ [NC]
RewriteRule (?!^domainone(/.*|)$)^.*$ /%1%{REQUEST_URI} [NC,L]
RewriteCond %{HTTP_HOST} ^(domaintwo)\.com$ [NC]
RewriteRule (?!^domaintwo(/.*|)$)^.*$ /%1%{REQUEST_URI} [NC,L]

Categories