Php HttAccess url重写用于ApacheLocalHost上不工作的漂亮url
我正在尝试将“漂亮”的URL应用到我的网站 ie:Php HttAccess url重写用于ApacheLocalHost上不工作的漂亮url,php,apache,.htaccess,url-rewriting,Php,Apache,.htaccess,Url Rewriting,我正在尝试将“漂亮”的URL应用到我的网站 ie:localhost/api/?id=15 到 以下是我的访问权限: RewriteEngine On RewriteBase /api/ RewriteRule ^/?id/([^/d]+)/?$ index.php?id=$1 [L,QSA] 似乎没有什么工作,但htaccess在这里看起来很好 我需要更新任何apache配置吗?正如@Abhishek在评论中提到的那样,如果它存在并且运行良好,你可以毫无意义地重新发明它 require 'A
localhost/api/?id=15
到
以下是我的访问权限:
RewriteEngine On
RewriteBase /api/
RewriteRule ^/?id/([^/d]+)/?$ index.php?id=$1 [L,QSA]
似乎没有什么工作,但htaccess在这里看起来很好
我需要更新任何apache配置吗?正如@Abhishek在评论中提到的那样,如果它存在并且运行良好,你可以毫无意义地重新发明它
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。对于类似的URL
localhost/api/id/15
RewriteEngine On
RewriteBase /api/
RewriteRule ^id/([0-9]+)$ index.php?id=$1 [L,QSA]
htaccess可以是这样的
localhost/api/id/15
RewriteEngine On
RewriteBase /api/
RewriteRule ^id/([0-9]+)$ index.php?id=$1 [L,QSA]
在
id=$1
中,您将获得id=15
此htaccess看起来不错。未启用htaccess覆盖和apache重写模块。
我不得不编辑文件/etc/apache2/apache2.conf
:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
启用apache重写模块
最后,使用sudo
服务apache2 restart重新启动apache,以查看操作中的更改。此解决方案与您所要求的不同,但它将为您提供更好的对这些漂亮url的管理。谢谢你的建议,但我想用.htacces执行此操作。@abhishekw这是你的htaccess文件吗?在文档root?.htaccess location/var/www/html/api@JonLinI中找到了导致问题的原因。未启用htaccess覆盖和apache重写模块。我的htaccess文件很好,现在正在运行。然后,您可以安全地删除该问题,因为它对其他人没有用处。我已更新了该问题,希望这对面临相同问题的其他人有帮助。执行此操作后,将出现500个内部服务器错误。。。。
sudo a2enmod rewrite