htaccess - 尾随斜杠网址

时间:2012-12-12 23:10:58

标签: php .htaccess mod-rewrite url-rewriting

您好我有以下问题:我有一些页面(php包含)我加载它们

http://www.domain.com/news/
他们工作得很完美。但是如果我删除尾部斜杠

http://www.domain.com/news

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

以下是我的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。可以吗,因为我在根文档中也有一个新闻文件夹?

2 个答案:

答案 0 :(得分:1)

是的,这是因为根目录中有一个news/文件夹。您的重写条件正在查找不是文件(即!-f)或目录(!-d)的任何内容。尝试重命名根目录中的news/目录。

如果必须,您可以通过执行以下操作强制domain.com/news重写为domain.com/news/

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

答案 1 :(得分:0)

这是一个mod_dir问题。当它认为URL映射到目录时(即使稍后确定不会),它会重定向浏览器,以确保在末尾附加尾部斜杠。 See this recent explanation that I posted in another question

与其他答案一样,您可以关闭DirectorySlash或确保所有受影响的网址都通过mod_rewrite重定向到尾部斜杠(以便重写重定向发生在同一个模块中):

  1. 通过添加DirectorySlash Off来关闭mod_dir。这使得mod_dir不会重定向浏览器,而是note that there are other consequences to turning this off。您可以在htaccess文件中添加该指令。

  2. 处理mod_rewrite中的尾部斜杠:

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