我有一个URL https://www.localhost/sitpoint/cellphone?id=Apple
我想把cellphone?id=Apple
转换成/Apple
像https://www.localhost/sitpoint/Apple
使用。htaccess url重写代码。
我希望能够更多地了解你想要完成什么,你不能简单地做到这一点很容易,你必须考虑很多其他因素,但这里有一个代码,你可以使用,让Apache知道,无论进入第一个URL参数,作为一个变量的值::
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?id=$1 [QSA,L]
在你的index.php文件中:(或你要重定向到的任何其他文件)
if(isset($_GET["id"])) {
//Split URL by '/' character, putting each one inside an array
$_URL = explode("/", filter_var(rtrim($_GET["id"], "/"), FILTER_SANITIZE_URL));
$id = $_URL[0];
//if user inputs www.myaapp.com/Apple
//$id would be = 'Apple'
}