简体   繁体   English

向其中添加查询字符串行后,URL重写不起作用

[英]URL rewriting doesn't work right after adding a query string line into it

This URL rewriting lines work fine till the query string line , and after the query string any of them not work. 此URL重写行可以正常工作, 直到查询字符串行为止 ,并且在查询字符串之后,它们中的任何一个都不起作用。

ReWriteEngine On
ReWriteRule ^home?$ /mysite/index.php
ReWriteRule ^gallery?$ /mysite/gallery.php
ReWriteRule ^([a-z0-9]+)?$ /mysite/owner.php?name=$1
ReWriteRule ^about?$ /mysite/about.php
ReWriteRule ^location?$ /mysite/location.php

The first two lines home and gallery works as expected (including the third line query string ), but the other two lines about and location don't work. 前两行homegallery按预期工作(包括第三行查询字符串 ),但其余两行关于和位置不起作用。 If I requested those two pages with the rewritten name , the pages not comes to browser (the response is nothing, besides I am getting the same page with i am currently on - not even getting a 404 error message), but if I requested those pages with the original name with the .php extension the page comes to the browser. 如果我要求使用重写的名称来显示这两页,则 这些页面不会出现在浏览器中 (响应什么都没有,除了我得到的页面与我当前所在的页面相同-甚至没有收到404错误消息),但是如果我要求这些页面原始名称带有.php扩展名的页面该页面进入浏览器。 I tried putting those two about and location at the top of the query string line, then those two pages work fine as expected , but when they are below of the query string line then only they don't work. 我尝试将这两个about和location 放在查询字符串行的顶部,然后这两个页面按预期工作正常 ,但是当它们在查询字符串行的下面时,只有它们不起作用。 Can I know what is wrong with the URL Rewriting methoed? 我能知道URL重写方法有什么问题吗? I still can't able to figure the problem out. 我仍然无法解决问题。

There are a number of issues with your attempt, here is a slightly modified version: 您的尝试有很多问题,这是一个稍作修改的版本:

ReWriteEngine On
ReWriteRule ^/?home/?$ /mysite/index.php [END]
ReWriteRule ^/?gallery/?$ /mysite/gallery.php [END]
ReWriteRule ^/?about/?$ /mysite/about.php [END]
ReWriteRule ^/?location/?$ /mysite/location.php [END]
ReWriteRule ^([a-z0-9]+)/?$ /mysite/owner.php?name=$1 [END]

The order of rules is important. 规则的顺序很重要。 The more general ones should be placed below the more specialized ones. 更一般的应该放在更专业的下面

In case you experience a http status 500 (server internal error) with above setup chances are that you operate a very old version of the apache http server. 如果您遇到http状态500(服务器内部错误)的情况,并且上述设置机会是您使用的Apache http服务器的版本非常旧。 In that case replace the [END] flag with the [L] flag which should also work fine in this scenario. 在那种情况下,用[L]标志替换[END]标志,在这种情况下也应该可以正常工作。


Above rules will work likewise in the real http servers host configuration and in dynamic configuration file. 以上规则在实际的http服务器主机配置和动态配置文件中同样起作用。

And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files ( .htaccess style files). 还有一个一般性提示:您应该始终喜欢将此类规则放置在http服务器(虚拟)主机配置中,而不是使用动态配置文件( .htaccess样式文件)。 Those files are notoriously error prone, hard to debug and they really slow down the server. 众所周知,这些文件容易出错,难以调试,并且确实降低了服务器的速度。 They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare). 仅当您无法控制主机配置(阅读:真正便宜的托管服务提供商)或者您的应用程序依赖于编写自己的重写规则(这显然是安全噩梦)时,才将它们作为最后一个选项来支持。 )。

I would change the order of these 我会改变这些顺序

ReWriteRule ^([a-z0-9]+)?$ /mysite/owner.php?name=$1
ReWriteRule ^about?$ /mysite/about.php
ReWriteRule ^location?$ /mysite/location.php

To

ReWriteRule ^about$ /mysite/about.php [L]
ReWriteRule ^location$ /mysite/location.php [L]
ReWriteRule ^([a-z0-9]+)$ /mysite/owner.php?name=$1 [L]

And add the last flag. 并添加最后一个标志。

what happens is that the ^([a-z0-9]+)? ^([a-z0-9]+)? is more generic and matches and changes the URL before the last 2 rules are reached. 更通用,可以在到达最后2条规则之前匹配并更改URL。

In other words, if you had a URL with about it matches this ^([a-z0-9]+)? 换句话说,如果您的URL与其相关about则匹配此^([a-z0-9]+)? gets changed and then does not match the actual rule for it. 被更改,然后与实际规则不匹配。 So by putting the more generic rule last you can avoid this. 因此,通过将更通用的规则放在最后,您可以避免这种情况。

Also as I said in the comments, I would add the [L] flag in or Last so that it ends the rewriting once a match is done, otherwise you can get unexpected matches. 就像我在评论中所说的,我会在[L] Last [L]或[ Last [L]添加[L]标志,这样一旦完成匹配,它将结束重写,否则您将获得意外的匹配。

You can also combine some of these (probably) 您也可以结合其中一些(可能)

 ReWriteRule ^(about|location)$ /mysite/$1.php [L]

I am pretty sure that the ? 我很确定? means the preceding match is optional, but I removed it because if not then the capture group is optional. 表示前面的匹配是可选的,但是我删除了它,因为如果没有,则捕获组是可选的。 So I am not sure what the purpose you had of having it there. 所以我不确定您在那里拥有它的目的是什么。 Because as I read it this means ^about?$ with the t being optional... 因为在我阅读时,这意味着^about?$ ,而t是可选的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM