这个工作对我来说:
的.htaccess
RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ profile.php?county=$1&name=$2&id=1
profile.php
<h1>Profile</h1>
<?php
echo "County: " . $_GET['county'] . "<br>";
echo "Name: " . $_GET['name'] . "<br>";
echo "Id: " . $_GET['id'];
此网址:http://localhost/tests/htaccess_test/Cheshire/Martin-Mack
生产THI小号HTML:
<h1>Profile</h1>
County: Cheshire<br>Name: Martin-Mack<br>Id: 1
![Rendering in Opera](https://i.stack.imgur.com/uhAsI.png)
测试在Apache 2.2.21和5.3.10 PHP。
在我的测试,我已经把两个文件在http://localhost/tests/htaccess_test/
,但你可以把它们放在所需;他们只需要在同一个文件夹中。
在你的情况,例如,你应该把这些文件放在你网站的根,所以,当你访问http://www.yoursite.com/Cheshire/Martin-Mack
,重定向将工作。
如果您仍然无法得到它的工作,尝试RewriteEngine On
之后加入RewriteBase /
。
完整的示例:
RewriteEngine On
RewriteBase/
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ profile.php?county=$1&name=$2&id=1
更新:在你的原代码,该id
参数是静态的。为了得到它的工作,试试这个:
RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ profile.php?county=$1&name=$2&id=$3
您可以尝试用(。*)替换([^ /] *),并确保您尝试重写的URL具有尾部斜线。 –
运气不好@SamuelCook – Liam
你想把'/ x/y /'改写成'/profile.php?county = x&name = y&id = 1',或者反过来呢? – nneonneo