Php 将MVC URL重写到不同的目录
目前,我有一个PHP MVC项目,需要重新编写URL以提高可用性。但是,该项目可以安装在不同的目录中,我无法编写一个正确的working.htaccess来处理这两种情况:Php 将MVC URL重写到不同的目录,php,apache,.htaccess,mod-rewrite,model-view-controller,Php,Apache,.htaccess,Mod Rewrite,Model View Controller,目前,我有一个PHP MVC项目,需要重新编写URL以提高可用性。但是,该项目可以安装在不同的目录中,我无法编写一个正确的working.htaccess来处理这两种情况: /var/www/html/testapp/-直接访问:127.0.0.1/testapp /var/www/html/specific_installation_number_4-虚拟主机:www.testapp4.org 如您所见,我必须使用不同的基本URI。项目结构如下所示: testapp/ index.ph
testapp/
index.php
.htaccess
public/
css/
style.css
js/
script.js
vendor/
frameworks/
jquery/
twitter/
bootstrap/
# Enable rewriting
RewriteEngine On
# Skip the public and vendor directories
RewriteRule ^(public|vendor)($|/) - [L]
# Skip existing files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the URL
RewriteRule ^(.*)$ index.php [QSA,L]
index.php:
<?php
echo("GET: ");
var_dump($_GET);
echo("<br>URI: " . $_SERVER["REQUEST_URI"]);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="vendor/frameworks/jquery/jquery.min.js"></script>
<script src="vendor/twitter/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="public/js/script.js"></script>
<link href="vendor/twitter/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="public/css/style.css" rel="stylesheet">
<title>Testapp</title>
</head>
<body>
<h1>Testapp</h1>
<p>Not implemented yet!</p>
</body>
</html>
因此,我们的想法是请求URI应该具有以下值:
(empty) (System will use the fallback controller)
mycontroller
mycontroller/action
mycontroller/action/42
问题:
最后,我真的放弃了从两个不同的目录(即两个不同的基本URI)重写的可能性。我刚刚在本地系统(www.test.com)中添加了一个虚拟主机。对于感兴趣的人,.htaccess如下所示:
testapp/
index.php
.htaccess
public/
css/
style.css
js/
script.js
vendor/
frameworks/
jquery/
twitter/
bootstrap/
# Enable rewriting
RewriteEngine On
# Skip the public and vendor directories
RewriteRule ^(public|vendor)($|/) - [L]
# Skip existing files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the URL
RewriteRule ^(.*)$ index.php [QSA,L]
注意:额外文件(如js、css、图像等)的输入源必须是绝对的:
<link href="/public/css/style.css" rel="stylesheet">