使用 mod_rewrite 重写 URL 以提供 RESTful URL

发布于 2024-08-25 13:05:57 字数 857 浏览 4 评论 0原文

Web 服务器是 Apache。我想重写 URL,这样用户就不会知道实际的目录。例如: 原网址:

www.mydomainname.com/en/piecework/piecework.php?piecework_id=11

预期网址:

piecework.mydomainname.com/en/11

我在.htaccess中添加了以下语句:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
RewriteRule ^(w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]

当然,我用我的域名替换了mydomainname。 .htaccess 放置在站点根目录中,但是当我访问piecework.mydomainname.com/en/11 时,我得到“找不到对象”。(当然我用我的域名替换了mydomainname。)

我在 .htaccess 中添加了以下语句。 htaccess:

RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3

当然我用我的域名替换了mydomainname。 .htaccess 放置在站点根目录中,但是当我访问piecework.mydomainname.com/en/11 时,我得到“找不到对象”。(当然我用我的域名替换了mydomainname。)

这是怎么回事?

The web server is Apache. I want to rewrite URL so a user won't know the actual directory. For example:
The original URL:

www.mydomainname.com/en/piecework/piecework.php?piecework_id=11

Expected URL:

piecework.mydomainname.com/en/11

I added the following statements in .htaccess:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
RewriteRule ^(w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]

Of course I replaced mydomainname with my domain name.
.htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)

I added the following statements in .htaccess:

RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3

Of course I replaced mydomainname with my domain name.
.htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)

What's wrong?

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

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

发布评论

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

评论(3

流绪微梦 2024-09-01 13:05:57

尝试在您的虚拟主机或服务器配置中按顺序使用 RewriteLog检查重写过程。现在你似乎只是猜测 mod_rewrite 的作用。

通过使用 RewriteLogLevel 您可以修改日志记录的扩展。对于初学者,我建议使用级别 5。

PS:修改配置后不要忘记重新加载/重新启动服务器。

Try using RewriteLog in your vhost or server-conf in order to check the rewriting process. Right now you just seem to guess what mod_rewrite does.

By using RewriteLogLevel you can modify the extend of the logging. For starters I'd recommend level 5.

PS: Don't forget to reload/restart the server after modifying the config.

围归者 2024-09-01 13:05:57

以下是对正在发生的事情的快速概述:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]

首先,问号应该位于末尾。

$1 会(应该)匹配任何非 'www' 0 或 1 次。

$2 与非字符的任何内容匹配 1 次或多次,理论上会匹配那里的空格,但可能永远不会匹配任何内容。

然后在这两个分组之后需要“.mydomainname.com”。

您的前两个条件是寻找两个单独的分组。

我不确定您到底如何尝试设置结构,但这是我根据您的原始 URL 和预期 URL 编写的方式:

RewriteCond %{HTTP_HOST} !^www\.mydomainname\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomainname\.com$ [NC]
RewriteRule ^(\w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]

基本上,您的第一个条件是确保它不是以“www”开头的 URL '(将其设为单独的规则会更容易)。第二个条件使其检查可能位于您域名前面的任何单词。然后你的重写规则将适当地重定向它。

去掉你问题中最后一个 .htaccess 行...

如果我输入错误,请有人纠正我。我不记得是否必须在“\w”和“\d”前面添加“\”,但无论如何我都包含了它们。

Here's a quick overview of what's happening:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]

First, the question mark is supposed to be at the end.

$1 would (should) match anything that is not 'www' 0 or 1 times.

$2 matches anything that is not a character 1 or more times, which theoretically would match a blank space there but likely would never match anything.

Then it requires '.mydomainname.com' after those two groupings.

Your first two conditions are looking for two separate groupings.

I'm not sure exactly how you're trying to set up your structure, but here is how I would write it based on your original and expected URL's:

RewriteCond %{HTTP_HOST} !^www\.mydomainname\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomainname\.com$ [NC]
RewriteRule ^(\w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]

Basically, your first condition is to make sure it's not the URL beginning with 'www' (it's easier to just make it a separate rule). The second condition makes it check any word that could possibly be in front of your domain name. Then your rewrite rule will redirect it appropriately.

Get rid of the last .htaccess line there in your question...

Someone please correct me if I typed something wrong. I don't remember if you have to have the '\' in front of '\w' and '\d' but I included them anyways.

盗心人 2024-09-01 13:05:57

你是在倒退。这个想法是,您将为人们提供友好的地址,并且重写规则会将对该友好的、不存在的页面的请求指向真实的页面,而他们不会看到它。所以现在你只能处理当他们访问丑陋的 URL 时要做什么,但你要输入友好的 URL。因为当人们直接输入友好的 URL 时不存在任何规则,apache 会查找它并说“未找到对象”

所以添加一行:

RewriteRule piecework.mydomainname.com/en/(*.) ^/$3/en/piecework/$3?piecework_id=([0-9]+)(.*)

抱歉,这是完全正确的,但基本思想是,如果他们输入您喜欢的 URL, Apache 已准备好重定向到真实页面,而浏览器不会看到它。

更新

我太困了,无法正确执行正则表达式,所以我只是尽力移动您的示例,抱歉。我会先尝试一些更简单的事情,以便首先了解基本概念。尝试一下:

RewriteRule www.mydomainname.com/en/piecework/piecework\.php\?piecework_id\=11 piecework.mydomainname.com/en/11

至少,如果出现错误,会更容易发现哪些地方不起作用。

You are doing it backwards. The idea is that you will give people the friendly address, and the re-write rule will point requests to this friendly, non-existent page to the real page without them seeing it. So right now you have it only handling what to do when they go to the ugly URL, but you are putting in the friendly URL. since no rule exists for when people put the friendly URL directly, apache looks for it and says "Object not Found"

So add a line:

RewriteRule piecework.mydomainname.com/en/(*.) ^/$3/en/piecework/$3?piecework_id=([0-9]+)(.*)

Sorry, that's quite right, but the basic idea is, if they put in the URL you like, Apache is ready to redirect to the real page without the browser seeing it.

Update

I'm way to sleepy to do regex correctly, so I had just tried my best to move your example around, sorry. I would try something more simple first just to get the basic concept down first. Try this:

RewriteRule www.mydomainname.com/en/piecework/piecework\.php\?piecework_id\=11 piecework.mydomainname.com/en/11

At the very least, it will be easier to see what isn't working if you get errors.

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