.htaccess 漂亮的网址 [英] .htaccess pretty urls

查看:26
本文介绍了.htaccess 漂亮的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 htaccess 使我的 URL 变得漂亮.

I would like to make my URLs pretty with htaccess.

我只有一个变量 id.所以页面就像 index.php?id=1index.php?id=2
我希望它们成为类似于 index/1/index/2/ 的东西——比如文件夹,而不是 id 变量...

I have only one variable id. So the pages are like index.php?id=1, index.php?id=2 etc.
What I would like them to be is something like index/1/, index/2/ - like folders, instead of the id variable...

如何使用 htaccess 重写 URL 来实现这一点?

How can I accomplish that using htaccess rewrite URLs?

我试图这样做:

<IfModule mod_rewrite.c>
 RewriteCond %{SCRIPT_FILENAME} !-d
 RewriteCond %{SCRIPT_FILENAME} !-f
 RewriteRule ^([^/]+) index.php?id=$1[L]
</IfModule>

推荐答案

这对我有用:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]+) index.php?id=$1 [L]

如果 index.php 文件是这样的:

if the index.php file is something like this:

<?php
import_request_variables('g', 'req_');
var_dump($req_id);

并且您向 http://domain/1

然后 index.php 有变量 req_id=1(因为 import_request_variables 调用)

then index.php has the variable req_id=1 (because of the import_request_variables call)

这篇关于.htaccess 漂亮的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆