如何使用 htaccess 从 URL 中删除变量名称?

发布于 2024-09-08 20:44:43 字数 267 浏览 3 评论 0原文

所以基本上我希望用户能够使用类似 /45678 的 URL 访问我的网站,而不是必须使用 /?p=45678,所以我真的只是想删除变量名称。我尝试过使用 mod_rewrite,但似乎仅用于在访问页面时删除名称。

这是当前的代码:

RewriteEngine On 

RewriteCond %{QUERY_STRING} ^p=([0-9]+=$

RewriteRule ^/$ /%1 [R]

So basically I want users to be able to go to my website with a URL of something like /45678, instead of having to use /?p=45678, so really I just want to remove the variable name. I've tried using mod_rewrite, but it seems that is only for removing the name when the page is visited.

Here is the current code:

RewriteEngine On 

RewriteCond %{QUERY_STRING} ^p=([0-9]+=$

RewriteRule ^/$ /%1 [R]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染墨丶若流云 2024-09-15 20:44:43

只需将所有链接更改为 /45678 而不是 ?p=45678。还是我完全误解了你的意思?因为我从您的帖子中得到的是它可以正常工作,除非您手动访问 ?p=45678,因为它保持为 ?p=45678。

编辑:
这就是我用于 http://www.madphp.org/dev/ 的内容,给出尝试一下,对我来说就像一个魅力(它还删除了index.php部分)。要访问现在更干净的 URL,您只需分解 $_SERVER['PATH_INFO'] 变量即可获取 PHP 脚本中的所有必需参数。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Simply change all of your links to /45678 rather than ?p=45678. Or did I misunderstand you completely? Because what I got from your post is that it works properly, unless you manually access the ?p=45678 where as it stays as ?p=45678.

EDIT:
This is what I am using for http://www.madphp.org/dev/, give it a go, works like a charm for me (it also removes the index.php part). To access your now cleaner URL you would simply explode the $_SERVER['PATH_INFO'] variable to get all of the required parameters within your PHP script.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
一刻暧昧 2024-09-15 20:44:43

你是否正确设置了mod_rewrite?如果是这样,您可以使用简单的 $_GET 变量之类的变量,在这种情况下,您必须在 PHP 中访问 $_GET['p']

Have you set up mod_rewrite correctly? If so, you can use variables like simple $_GET variables, in this case you must access $_GET['p'] in PHP.

谁许谁一生繁华 2024-09-15 20:44:43

我没有使用 .htaccess 来完成此操作,但它确实查询了数据库。我不久前写了这个,所以它使用 PEAR DB,调整到您的数据库/连接方法。我将复制我的代码,然后让您找出需要进行哪些更改。

$db=connect_db();
$name=substr($_SERVER['PHP_SELF'], 20);
$name=strtolower($name);
$id=$db->getone("select id from user where login='{$name}'");
header("Location: /dragonart/profile?user=" . $id);

如果您将信息存储在数据库中,这可能是一个不错的选择。缺点是 URL 不会被重写,用户最终会被发送到以 $_GET 变量结尾的页面。

编辑:
刚刚意识到使用我的方法可以使用更简单的方法来获得答案。由于我的解决方案用于使用用户名查找用户的 id,然后将某人发送到他们的个人资料(需要 id),因此更好的解决方案如下所示:

$var=substr($_SERVER['PHP_SELF'], $length);  
header("Location: /path/to/page?p=".$var);

其中 $length 是不带变量的 URL 的通常长度结束。

I did this without using .htaccess, but it does query a database. I wrote this a while ago so it uses PEAR DB, adjust to your database/connection method. I'll just copy my code and let you figure out what changes you need.

$db=connect_db();
$name=substr($_SERVER['PHP_SELF'], 20);
$name=strtolower($name);
$id=$db->getone("select id from user where login='{$name}'");
header("Location: /dragonart/profile?user=" . $id);

If you store your information in a database this may be a nice alternative. The downside is that the the URL is not rewritten and the user is ultimately sent to a page with ending in a $_GET variable.

edit:
Just realized that using my method a simpler method can be used for the answer. Since my solution was used to find the id of a user using their username and then send someone to their profile (which requires the id) a better solution would be something like:

$var=substr($_SERVER['PHP_SELF'], $length);  
header("Location: /path/to/page?p=".$var);

where $length is the usual length of the URL without the variable at the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文