I'm not sure how you call it, but could anyone help me with this problem: Is there a way to set a .php file to a directory.
Example: I have 2 files in the same directory: onepage.php and secondpage.php. If I want to visit index.php I would go to ..../onepage.php (lets say: www.stackoverflow.com/onepage.php). Then, if I want to visit the second page I would go to www.stackoverflow.com/secondpage.php. However, is there a way to make it so it's visible as www.stackoverflow.com/secondpage?
Also, if I take the same example, and let's say I have a $_GET at my secondpage.php, is there also a way to do www.stackoverflow.com/secondpage/($_GET value) ?
Thanks.
This can be achieved using mod_rewrite
for your Apache server and .htaccess
to set the rules to effectively rewrite the visible URL.
Use mod_rewrite
in .htaccess
file.Like this code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule secondpage/(.*)$ index.php?var1=$1 [L]
Now $_GET['var1']
is your variable. Good luck.
THIS IS WORKING CODE COPIED FROM MY WEBSITE
create a file called .htaccess
add this code:
RewriteEngine On
# Run everything else but real files through parse.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|png|html|js|json|jpg|jpeg)$
RewriteRule ^(.*)$ parse.php?info=$1 [L]
Now in your parse.php file have this code:
<?php
$getVars = $_GET['info'];
$vars = explode("/",$getVars);
if(count($vars)<1){
$vars[0] = "home";
$vars[1] = "index";
}else if(count($vars)<2){
$vars[1] = "index";
}
$fileString = $vars[0] . '/' . $vars[1] . '.php';
if(file_exists($fileString)){
include_once($fileString);
}else{
include_once("error/404.php");
}
Now add file like:
directory: mydirectory file: index.php
Access as myurl.com/mydirectory/