简体   繁体   English

htaccess-斜杠网址

[英]htaccess - trailing slash url

Hello I have the following problem: I have some pages (php includes) which I load them like this 您好,我遇到以下问题:我有一些页面(包括PHP),我像这样加载它们

http://www.domain.com/news/

they work perfect. 他们工作完美。 But if I remove the trailing slash 但是如果我删除结尾的斜杠

http://www.domain.com/news

this happens -> http://www.domain.com/news/?page=news&request= 发生这种情况-> http://www.domain.com/news/?page=news&request=

Here are my htaccess rules: 这是我的htaccess规则:

RewriteEngine on

<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl|jpg|png|gif)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>




Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)(.*)$ /index.php?page=$1&request=$2

PS. PS。 Could it by because I have a news folder in the root document as well ? 是否可以因为我在根文档中也有一个新闻文件夹?

Yes, it's because you have a news/ folder in the root. 是的,这是因为您的根目录中有一个news/文件夹。 Your Rewrite Condition is looking for anything that isn't a file (ie !-f ) or a directory ( !-d ). 您的重写条件正在寻找不是文件(即!-f )还是目录( !-d )之外的任何内容。 Try renaming your news/ directory in the root. 尝试在根目录中重命名news/目录。

If you must, you can force domain.com/news to rewrite to domain.com/news/ by doing the following: 如果需要,可以通过执行以下操作来强制domain.com/news重写为domain.com/news/

RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

This is a mod_dir issue. 这是一个mod_dir问题。 It's redirecting the browser when it thinks a URL maps to a directory (even if it's later determined not to) to ensure that a trailing slash is appended to the end. 当认为URL映射到目录(即使后来确定不匹配)时,它将重定向浏览器,以确保在末尾附加斜杠。 See this recent explanation that I posted in another question . 请参阅我在另一个问题中发布的最新解释

Like in the other answer, you can either turn off DirectorySlash or ensure that all of the affected URL's get redirected with a trailing slash via mod_rewrite (so that the rewrite and the redirect happens within the same module): 就像在其他答案中一样,您可以关闭DirectorySlash或确保所有受影响的URL都通过mod_rewrite以尾部斜杠进行重定向(这样,重写重定向将在同一模块内进行):

  1. Turn off mod_dir by including a DirectorySlash Off . 通过包含DirectorySlash Off关闭mod_dir。 This makes it so mod_dir won't redirect the browser, but note that there are other consequences to turning this off . 这样一来,mod_dir就不会重定向浏览器,但是请注意,关闭它还会带来其他后果 You can add that directive in your htaccess file. 您可以在htaccess文件中添加该指令。

  2. Handle the trailing slash in mod_rewrite: 在mod_rewrite中处理斜杠:

     RewriteEngine On Options -MultiViews RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !/$ RewriteRule ^(.*)$ /$1/ [L,R=301] 

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

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