php 带有 .htaccess 的漂亮 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25080835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:38:50  来源:igfitidea点击:

Pretty URLs with .htaccess

phpapache.htaccessmod-rewrite

提问by Port 8080

I have a URL http://localhost/index.php?user=1. When I add this .htaccessfile

我有一个网址http://localhost/index.php?user=1。当我添加这个.htaccess文件时

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/(.*)$ ./index.php?user=

I will be now allowed to use http://localhost/user/1link. But how about http://localhost/index.php?user=1&action=updatehow can I make it into http://localhost/user/1/update?

我现在将被允许使用http://localhost/user/1链接。但是http://localhost/index.php?user=1&action=update我怎样才能把它变成http://localhost/user/1/update

Also how can I make this url http://localhost/user/add? Thanks. Sorry I am relatively new to .htaccess.

另外我怎样才能制作这个网址http://localhost/user/add?谢谢。抱歉,我对.htaccess.

采纳答案by Port 8080

Thanks for the idea @denoise and @mogosselin. Also with @stslavik for pointing out some of the drawback of my code example.

感谢@denoise 和@mogosselin 的想法。还有@stslavik 指出我的代码示例的一些缺点。

Here's how I do it:

这是我的方法:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/([0-9]*)/([a-z]*)$ ./index.php?user=&action=
RewriteRule ^user/([a-z]*)$ ./index.php?user&action=

by using var_dump($_GET);on the link localhost/user/1234/updateI got

通过var_dump($_GET);localhost/user/1234/update我得到的链接上使用

array (size=2)
  'user' => string '1234' (length=4)
  'action' => string 'update' (length=3)

while localhost/user/add

尽管 localhost/user/add

array (size=2)
  'user' => string '' (length=4)
  'action' => string 'update' (length=3)

which is my goal. I will just only do other stuffs under the hood with PHP.

这是我的目标。我只会在幕后用 PHP 做其他事情。

回答by denoise

you can write something like this:

你可以这样写:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?user=&action= [L]

回答by mogosselin

If you want to turn

如果你想转

http://www.yourwebsite.com/index.php?user=1&action=update

http://www.yourwebsite.com/index.php?user=1&action=update

into

进入

http://www.yourwebsite.com/user/1/update

http://www.yourwebsite.com/user/1/update

You could use

你可以用

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/([0-9]*)/([a-z]*)$ ./index.php?user=&action=

To see the parameters in PHP:

在 PHP 中查看参数:

<?php 
echo "user id:" . $_GET['user'];
echo "<br>action:" . $_GET['action'];
?>
  • The parenthesis in the .htaccess are groups that you can call later. with $1, $2, etc.
  • The first group I added ([0-9]*) means that it will get any numbers (1, 34, etc.).
  • The second group means any characters (a, abc, update, etc.).
  • .htaccess 中的括号是您可以稍后调用的组。1 美元、2 美元等。
  • 我添加的第一组 ([0-9]*) 表示它将获得任何数字(1、34 等)。
  • 第二组表示任何字符(a、abc、update 等)。

This is, in my opinion, a little bit more clean and secure than (.*) which basically mean almost anything is accepted.

在我看来,这比 (.*) 更干净和安全,这基本上意味着几乎任何东西都可以被接受。

回答by stslavik

Since you tagged this with PHP, I'll add a little perspective from what I did, and it may or may not help you.

既然你用 PHP 标记了这个,我会从我所做的事情中添加一些观点,它可能会也可能不会帮助你。

You can, of course, write solely in .htaccess, being careful about order. For instance, let's say that you have:

当然,您可以仅在 .htaccess 中编写,注意顺序。例如,假设您有:

RewriteRule ^user/([0-9]+)/update$ ./index.php?user=&action=update
RewriteRule ^user/([0-9]+)$ ./index.php?user=

Then it should, upon receiving

那么它应该,在收到

http://localhost/user/1/update

http://localhost/user/1/update

go to

http://localhost/index.php?user=$1&action=update

http://localhost/index.php?user=$1&action=update

and not

并不是

http://localhost/index.php?user=$1

http://localhost/index.php?user=$1

Now, what I did instead was push everything to index.php?q=$1

现在,我所做的是把一切都推到 index.php?q=$1

RewriteRule ^(.*)$ index.php?q=

Then I used index.php to handle how the query was broken up. So let's say someone enters

然后我使用 index.php 来处理查询是如何分解的。所以假设有人进入

http://www.example.com/user/18239810/update

http://www.example.com/user/18239810/update

this would go to

这将去

http://www.example.com/index.php?q=user/18239810/update

http://www.example.com/index.php?q=user/18239810/update

From there, explode the query string along the first /to give userand 18239810/update.

从那里,沿着第一个/展开查询字符串以给出user18239810/update

This would tell me that I need to pass 18239810/updateto the usercontroller. In that controller, I again explode the argument into the user id and command, and I can switch on the command to tell how to load the page, passing the user id as an argument to the updatefunction.

这会告诉我我需要传递18239810/updateuser控制器。在那个控制器中,我再次将参数分解为用户 ID 和命令,我可以打开命令来告诉如何加载页面,将用户 ID 作为参数传递给update函数。

Very quick and dirty example (index.php):

非常快速和肮脏的例子(index.php):

<?php
$getString = explode('/', $_GET['q'], 1);
$controller = $getString[0].'Controller';
require_once('/controllers/'.$controller.'.php');
$loadedController = new $controller( $getString[1] );
?>

Of course, this means that constructors all must take a string argument that will be parsed for acceptable values. You can do this with explodes and switch statements, always defaulting back to the standard front page to prevent unauthorized access based on random guessing.

当然,这意味着构造函数都必须接受一个字符串参数,该参数将被解析为可接受的值。您可以使用爆炸和开关语句来做到这一点,始终默认返回标准首页,以防止基于随机猜测的未经授权的访问。

回答by Gavin

Its simple just try this out !

它很简单,试试这个!

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^game/([0-9]+)/([_-0-9a-zA-Z]+)/?$  index.php?user=&action= [L,NC]

Thats it !!

就是这样 !!

回答by Khalid

a simple way is to pass only one variabe to index.php like this

一种简单的方法是像这样只将一个变量传递给 index.php

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^(.*)$ index.php?data= [QSA]

and in your index.php file you do this

并在您的 index.php 文件中执行此操作

$data = expload("/",$_GET['data']);
$user = $data[1];
$action = $data[2];

this one works for all cases, when you try to pass many variables, it doesn't work in the wase when you do something like

这适用于所有情况,当您尝试传递许多变量时,当您执行类似操作时,它不起作用

http://localhost/user/add/12/54/54/66

the last variable always takes the value add/12/54/54/66

最后一个变量总是取值 add/12/54/54/66

回答by Kinnectus

For /user/addyou will need to do a separate rule because you have no "middle parameter". So:

/user/add因为您没有“中间参数”,因此您需要执行单独的规则。所以:

RewriteRule ^user/add$ ./index.php?action=add [L,QSA]

You can then do additional rules for URLs that contain additional parameters:

然后,您可以对包含附加参数的 URL 执行附加规则:

RewriteRule ^user/([0-9]+)/([A-Za-z]+)$ ./index.php?user=&action= [L,QSA]

This will allow you to perform actions on existing users. E.g. /user/1/update

这将允许您对现有用户执行操作。例如/user/1/update

回答by Hemal Halari

Try this it is very simple:

试试这个很简单:

Options +FollowSymLinks

RewriteEngine on

RewriteRule index/user/(.*)/(.*)/ index.php?user=&action=

RewriteRule index/user/(.*)/(.*) index.php?user=&action=