URL 使用 .htaccess 文件重写

URL rewrite using .htaccess file

我有一个URLhttps://www.localhost/sitpoint/cellphone?id=Apple 我想使用 .htaccess url 重写代码将 cellphone?id=Apple 转换为 /Apple 就像 https://www.localhost/sitpoint/Apple 一样。

我希望能够更多地了解你想要完成的事情,你不能简单地做到这一点,而且你必须考虑很多其他因素,但这里有一个代码你可以用来让 Apache 知道第一个 URL 参数中的任何内容都会作为变量的值::

Options -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?id= [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'

    }