【问题标题】:htaccess url rewrite for pretty url not working on Apache localhosthtaccess url 重写漂亮的 url 在 Apache localhost 上不起作用
【发布时间】:2018-06-17 23:51:54
【问题描述】:

我正在尝试将“漂亮”的 URL 应用到我的网站。

即:localhost/api/?id=15

localhost/api/id/15

这是我的 htaccess:

RewriteEngine On
RewriteBase /api/
RewriteRule ^/?id/([^/d]+)/?$ index.php?id=$1 [L,QSA]

似乎没有任何效果,但 htaccess 在这里看起来不错。

我需要更新任何 apache 配置吗?

【问题讨论】:

  • 此解决方案与您所要求的不同,但它可以让您更好地管理这些漂亮的 url。 AltoRouter
  • 感谢您的建议,但我想使用 .htacces .@Abhishek
  • 您的 htaccess 文件在哪里?在文档根目录中?
  • .htaccess location /var/www/html/api @JonLin

标签: php apache .htaccess url-rewriting


【解决方案1】:

这个 htaccess 看起来不错。 htaccess 覆盖和 apache 重写模块未启用。 我不得不编辑文件/etc/apache2/apache2.conf

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

并将其更改为;

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

那么,

sudo a2enmod rewrite

启用 apache 重写模块。

最后使用 sudo service apache2 restart 重新启动 apache 以查看变化。

【讨论】:

  • 执行此操作后出现 500 内部服务器错误....
【解决方案2】:

正如 @Abhishek 在 cmets 中提到的那样,您可以使用 AltoRouter. 如果它存在并且运行良好,则无需重新发明轮子。

require 'AltoRouter.php';
$r = new AltoRouter();
$r->map('/api/id/[i:id]', function($userid) {
    // your logic for what this URL does
    // [i:id] will look for any integers and store it as $userid
}, 'example');
$m = $r->match();
if($m && is_callable($m['target'])):
    call_user_func_array($m['target'], $m['params']);
else:
    header('Location: /404');
    exit();

您可以在网站上找到文档和示例 htaccess。

【讨论】:

    【解决方案3】:

    对于这样的网址

    localhost/api/id/15
    

    htaccess可以是这样的

    RewriteEngine On
    RewriteBase /api/
    RewriteRule ^id/([0-9]+)$ index.php?id=$1 [L,QSA]
    

    id=$1 你会得到id=15

    【讨论】:

    • 我找到了导致问题的原因。 htaccess 覆盖和 apache 重写模块未启用。我的 htaccess 文件很好,现在可以使用了。
    • 那么,您可以放心地删除该问题,因为它对其他人无用
    • 我已经更新了问题,希望这对面临同样问题的其他人有所帮助。
    猜你喜欢
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多