【问题标题】: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

【问题讨论】:

  • 听起来不错。考虑使用 mod_rewrite。

标签: 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'

    }

【讨论】:

    猜你喜欢
    • 2017-01-26
    • 2023-03-03
    • 2013-04-22
    • 2017-04-08
    • 2014-03-24
    • 1970-01-01
    • 2015-02-03
    • 2023-03-23
    相关资源
    最近更新 更多