【问题标题】:URL rewrite using .htaccess file [duplicate]使用 .htaccess 文件重写 URL [重复]
【发布时间】:2015-10-01 17:37:56
【问题描述】:
我有一个网址https://www.localhost/sitpoint/cellphone?id=Apple
我想使用 .htaccess url 重写代码将cellphone?id=Apple 转换为/Apple,如https://www.localhost/sitpoint/Apple。
【问题讨论】:
标签:
php
.htaccess
url
url-rewriting
rewrite
【解决方案1】:
我希望能够更多地了解你想要完成的事情,你不能简单地做到这一点,你必须考虑很多其他因素,但这里有一个代码你可以用来让 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'
}