网址mod,重写获取新页ID重写、网址、mod、ID

2023-09-02 01:09:19 作者:情意怎寄

我需要从专家的一些帮助。

i need some help from the experts.

我有一些重写规则:

post.php go to http://example.com/post/3/
RewriteRule ^post/([A-Za-z0-9-]+)/?$ post.php?id=$1 [NC,L]

它的作品!

不过,交/ ID后,我如何添加一个新的重写规则到新页面Users.php / 并获得Users.php的ID(Users.php?ID = 3)

But, How can i add a new rewrite rule to the NEW Page Users.php after post/ID/ And get the ID in Users.php (Users.php?id=3)

像这个 http://example.com/post/3/users

谢谢!

推荐答案

如果你想 /后/ 3 /用户/ / Users.php?ID = 3 ,你必须把这个规则现有规则之前。您现有的规则匹配 /后/ 3 / 这是这是什么额外的规则匹配preFIX,因此该规则将永远不会触发,如果它是后。

If you want /post/3/users/ to go to /Users.php?id=3, you have to put that rule before your existing rule. Your existing rule matches /post/3/' which is a prefix of what this additional rule matches, so that rule will never fire if it is after.

# catch the longer URL first
RewriteRule ^post/([A-Za-z0-9-]+)/users/?$ Users.php?id=$1 [NC,L]

# No /users/ on it; rewrite to post.php
RewriteRule ^post/([A-Za-z0-9-]+)/?$ post.php?id=$1 [NC,L]

另一件事:你标记的.htaccess 您的帖子。这是否意味着你重写在的.htaccess ?如果是这样,你应该使用的RewriteBase ,因为你的重写是相对的。为什么它的工作可能就是你允许1:在你的web服务器路径和URL之间的对应1

Another thing: you tagged your post with .htaccess. Does that mean your rewrites are in a .htaccess? If so, you should use RewriteBase because your rewrites are relative. Why it's working is probably that you are allowing a 1:1 correspondence between paths and URLs on your webserver.

在每个目录范围内如的.htaccess ,mod_rewrite的正与路径名,而不是URL。但是,如果你做一个相对重写,道路变成一个网址,并反馈到Apache的请求处理链被重新处理过。如何将路径转化为一个URL是的RewriteBase 的内容被添加到了前面。如果你没有的RewriteBase 那么愚蠢的事情发生了:该文件的目录(已删除了重写规则只是上涨回来!)。

In a per-directory context like .htaccess, mod_rewrite is working with path names, not URLs. But if you do a relative rewrite, the path is turned into a URL and fed back into the Apache's request handling chain to be processed over again. How the path is turned into a URL is that the contents of RewriteBase are added to the front. If you don't have RewriteBase then a silly thing happens: the path to your directory (that was removed for the RewriteRule is just tacked back on!).

例如:假设你的DocumentRoot是 /无功/网络。假设浏览器请求的URL /富。这被转换为路径 /无功/网络/富如果在的.htaccess 在/ var / WWW / 你重写(和的RewriteBase 未设置),则说明mod_rewrite将生成的URL /无功/网络/条:它只是需要的在/ var / WWW的剥离下来并把它放回/ 目录。现在,可以向工作:只是让 /无功/网络/ 有效的去到该目录的URL。例如以

Example: suppose your DocumentRoot is /var/www. Suppose the browser asks for the URL /foo. This gets translated to the path /var/www/foo If inside the .htaccess for /var/www/ you rewrite foo to bar (and RewriteBase is not set), then mod_rewrite will generate the URL /var/www/bar: it just takes the /var/www/ directory that was stripped off and puts it back on. Now that can be made to work: just make /var/www/ a valid URL going to that directory. For instance with

Alias /var/www /var/www # map /var/www URL to /var/www directory

但是,这是哈克。正确的方法是让的RewriteBase / 中的.htaccess。因此,当改写为,它只是得到了 / 在前面,成为链接 /条。这被反馈到服务器,并且将解析回文档根再次自然

But that is hacky. The right way is to have RewriteBase / in the .htaccess. So when foo is rewritten to bar, it just gets a / in front and becomes the url /bar. This is fed back to the server and will resolve back to the document root again "naturally".

我用这样的黑客在我理解了重写。我甚至用 / 的DocumentRoot !这让一切都工作了,因为当时大多数网址是路径:你没有想到的网址从你的文件系统路径的一种抽象独立的。但是,这是一个危险的,愚蠢的事情。

I used hacks like that before I understood the rewriting. I even used / as a DocumentRoot! That made everything work out since then most URLs are paths: you don't have to think of URLs a an abstraction separate from your filesystem paths. But it's a dangerous, silly thing.