我正在尝试解决一些htaccess问题,但我无法找到解决方案。我有以下html:
<a href="www.site.com/home.php?Idi="English">English</a>
<a href="www.site.com/home.php?Idi="Française">Française</a>
目标是用户只需进入其地址栏:
www.site.com/home/English
或
www.site.com/home/Française
显然,get 变量应该到达主页.php进行处理。
此外,该网站还有其他带有扩展.php的页面,我删除了每个扩展
: Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
此外,我想要起始页:
www.site.com/home.php
将显示为:
www.site.com/home/English
我看过很多关于它的教程和帖子,但我无法解决它。
任何帮助将不胜感激,谢谢
已编辑:
最后,正如UnsskillFreak建议的那样,我将原始链接更改为:
<a href="www.site.com/home/English">English</a>
<a href="www.site.com/home/Française">Française</a>
HTaccess为:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#Receive site.com/home/Language and internally processes site.com/home.php?Idi=Language
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^home/(English)/?$ home.php?Idi=English [QSA,NC,L]
RewriteRule ^home/(Française)/?$ home.php?Idi=Française [QSA,NC,L]
#Receive site.com/something and internally goes to site.com/something.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
为了避免新页面中加载的css,js和其他链接崩溃,我在html的每个路径之前都添加了一个/
。
htaccess 放置在根目录中。服务器中不存在"主"文件夹。
就像上面的评论解释和编辑的帖子一样,它会;)
但这是我得到的另一种解决方案,也可以工作:
在我的document_root中,我添加了一个名为 test
的文件夹,在那里我有 4 个包含以下内容的文件:
.htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /test/index.php [L]
index.php
- 用于重写和测试链接的包装器
echo'
<!DOCTYPE html>
<head>
<title>Rewrite Test</title>
<link rel="stylesheet" type="text/css" href="/test/style.css">
</head>
<body>
<a href="/test/home/english">English</a><br>
<a href="/test/home/francaise">Francaise</a><br>
';
$get = explode("/",$_SERVER["REQUEST_URI"]);
$get = array_slice($get,2); // ignoring first empty key and folder 'test'
#echo "<pre>".print_r($get,true)."</pre>";
if(file_exists($get[0].".php"))
require($get[0].".php");
else
echo 'file '.$get[0].'not found';
echo'
</body>
</html>';
home.php
- 根据要求
<?php
echo 'in home.php, you have choosen:'.$get[1];
?>
style.css
- 仅作为测试文件访问是否有效
body {
color:#ff8800;
font-weight:bold:
}
经过测试并按预期工作:)