问题
I would like to have url's like this
mysite.com/language/page/somethingelse
And processing that always in the same file which is index.php
.
But only for .php
files. I don't want to redirect .js
or .css
or .xml
, image files and so on.
Plus, this project will be in a subfolder, meaning it will be inside another with no relation whatsoever. Therefore, it will be accessible like this:
mysite.com/extras/mydifferentproject/language/page/something else
So I suppose the .htaccess
file inside mydifferentproject
folder will need this line?
RewriteBase extras/mydifferentproject/
回答1:
You can have this as a .htacces
RewriteEngine On
RewriteRule ^(?!index\.php$). index.php [NS,L,DPI]
Catching the value after the folder
From the folder that the .htacces
is in all the URL's
will be caught. In order to get the path and get if it's in a folder and process it further this function will help.
function getPath($path){
$called = __FILE__;
$called_dir = str_replace("\\","/",pathinfo($called)["dirname"]);
$root = str_replace($_SERVER["DOCUMENT_ROOT"], "",$called_dir);
$from = '/'.preg_quote($root, '/').'/';
return preg_replace($from,"",$path,1);
}
How to use
getPath($_SERVER["REQUEST_URI"]);
This will get you the string that is behind the folder it is in, so it excludes the folder it is in.
Example
.htacces: is in the folder /folder
url: example.com/folder/some/values
result of the getPath()
function in index.php
: /some/values
as it's in the folder folder
.
Not in folder
In the case that your URL example.com/index.html
is, but the .htacces
is in folder
the behavior is just normal. (will load index.html)
Important
When loading files through a process of handeling the path, or anything you do with it. When loading requesting files in the html
be sure to define the base
of where he files should be loaded from, otherwise it will take the path
input from above and see those as folders, and not as values or something it should not look at.
Example
<base href="/folder/">
回答2:
I could accomplish the following task send only .php
files to index.php like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,QSA]
Now I'm able to access images and js, css, xml files, etc, by writing their real url's in the browser's navbar.
Once in index.php I'm using Nytrix's function to parse the url.
来源:https://stackoverflow.com/questions/41667483/how-can-i-send-only-php-files-to-same-page