简体   繁体   English

删除网址中的斜杠(不包含斜杠)

[英]Removing slash in url (not trailing slash)

I have a folder named people on my server, and index.php in that folder My url is like mydomain.com/people/?name=value1&age=value2 我的服务器上有一个名为people的文件夹,该文件夹中有index.php。我的网址类似于mydomain.com/people/?name=value1&age=value2

But I really want it to look like mydomain.com/people?name=value1&age=value2 但我真的希望它看起来像mydomain.com/people?name=value1&age=value2

Since "people" is a folder and your script is in that folder, the only way for this to work is if you turn off DirectoryIndex , which automatically redirects the browser to include a trailing slash for any request that's for a folder. 由于“ people”是一个文件夹,而脚本位于该文件夹中,因此唯一可行的方法是关闭DirectoryIndex ,它会自动重定向浏览器,以便在任何针对该文件夹的请求中都包含一个斜杠。

Note, this is a trailing slash , the URI ends with /people/ . 注意,这是一个斜杠 ,URI以/people/结尾。 The ? ? and everything after it is the query string. 后面的所有内容都是查询字符串。

Turning off DirectoryIndex can be very dangerous, as it is used to prevent information disclosure. 关闭DirectoryIndex可能非常危险,因为它用于防止信息泄露。 Without a trailing slash on your folders, requesting a folder will result in displaying the contents of that folder even if you have a directory index . 如果文件夹中没有尾部斜杠,则请求文件夹将导致显示该文件夹的内容, 即使您具有目录索引也是如此 In other words, index.php is ignored and instead, you get a listing of all your folder's contents. 换句话说, index.php被忽略,而是获得所有文件夹内容的清单。 So to prevent that from happening, you have to internally add the slash back. 因此,为防止这种情况发生,您必须在内部添加反斜杠。

So something like this in the htaccess file of your document root: 因此,在文档根目录的htaccess文件中是这样的:

DirectoryIndex Off

RewriteEngine On

RewriteCond %{THE_REQUEST} \ /+people/\?
RewriteRule ^ /people [L,R]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [L]

Using mod_rewrite you can do it like this: 使用mod_rewrite可以这样:

RewriteEngine On
RewriteRule ^(.+)/$ $1 [L,R,QSA]

QSA here is not required since it stands for Query String Append and it's on by default. 此处不需要QSA ,因为它表示查询字符串追加,并且默认情况下处于启用状态。

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

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