我想改变我的网址。在这里,我有一个像这样的目录结构
htdocs/
example/
public/
login.php
people/
people1.php
people2.php
animal/
animal1.php
animal2.php
404.php
assets/
css/
js/
然后我想按照根目录
中的现有目录,如下所示localhost/example/login
localhost/example/people/people1
localhost/example/people/people2
localhost/example/animal/animal1
localhost/example/animal/animal2
我尝试使用以下内容制作 .htaccess 文件
Options +FollowSymLinks
RewriteEngine On
rewritecond %{REQUEST_URI} !^/public/(.*)
rewritecond %{REQUEST_URI} !^/assets/(.*)
RewriteRule .* index.php [L]
它是index.php
$requested = empty($_SERVER['REQUEST_URI']) ? false : $_SERVER['REQUEST_URI'];
switch ( $requested ) {
case '/login':
include 'public/login.php';
break;
default:
include 'public/404.php';
}
当我走向localhost / example / login时,目的地是 404.php (错误)。
你可以帮帮我吗?答案 0 :(得分:1)
$_SERVER['REQUEST_URI']
变量是整个 URI。因此,如果转到http://example.com/example/login
$_SERVER['REQUEST_URI']
变量为/example/login
。您可以尝试做的事情是将htaccess文件更改为:
Options +FollowSymLinks
RewriteEngine On
rewritecond %{REQUEST_URI} !/public/(.*)
rewritecond %{REQUEST_URI} !/assets/(.*)
RewriteRule ^(.*)$ index.php/$1 [L]
(请注意,^/public/
永远不会匹配,因为REQUEST_URI
将是/example/public
)
然后在您的代码中使用$_SERVER['PATH_INFO']
代替。
答案 1 :(得分:1)
看起来你正试图隐藏PHP扩展,优先级设置为:
将此代码放入DOCUMENT_ROOT/.htaccess
文件中:
RewriteEngine On
# skip for existing files/directories (/assets will be skipped here)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# try to load PHP file from public directory
RewriteCond %{DOCUMENT_ROOT}/public/$1.php -f
RewriteRule ^(.+?)/?$ /public/$1.php [L]
# now try .php elsewhere
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]