Apache .htaccess在页面上添加尾随斜杠,除非';在URL中有

Apache .htaccess在页面上添加尾随斜杠,除非';在URL中有,apache,.htaccess,mod-rewrite,expressionengine,Apache,.htaccess,Mod Rewrite,Expressionengine,我希望我的URL添加尾随斜杠,但仅当URL不以锚点结尾时 这是我当前的.htaccess文件 # ---------------------------------------------------------------------- # Enable Rewrite Engine # ---------------------------------------------------------------------- RewriteEngine On RewriteBase /

我希望我的URL添加尾随斜杠,但仅当URL不以锚点结尾时

这是我当前的.htaccess文件

# ----------------------------------------------------------------------
# Enable Rewrite Engine
# ----------------------------------------------------------------------

RewriteEngine On
RewriteBase /


# ----------------------------------------------------------------------
# Remove the www from the URL
# ----------------------------------------------------------------------

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


# ----------------------------------------------------------------------
# Add a trailing slash to paths without an extension
# ----------------------------------------------------------------------

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]


# ----------------------------------------------------------------------
# Remove index.php
# ----------------------------------------------------------------------

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

当我链接到锚定部分时,URL被写入:

http://mysite.com/subdir/#anchor
http://mysite.com/subdir#anchor
http://mysite.com/directory/sub/

当它们看起来像这样时:

http://mysite.com/subdir/#anchor
http://mysite.com/subdir#anchor
http://mysite.com/directory/sub/

常规URL应如下所示:

http://mysite.com/subdir/#anchor
http://mysite.com/subdir#anchor
http://mysite.com/directory/sub/


我被难住了,所以任何帮助都会很好

干杯,

不要认为这是因为你的重写规则。片段(URI末尾的
#something
)永远不会被发送到服务器,它们被用作浏览器呈现的页面内的从属资源

确保您提供的HTML在锚中不包含尾部斜杠,或者您没有在浏览器端附加斜杠的javascript


编辑:

好的,如果要删除URI末尾添加的斜杠,您可能需要注释掉以下规则:

# ----------------------------------------------------------------------
# Add a trailing slash to paths without an extension
# ----------------------------------------------------------------------

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

你看过Apache的设置吗

下面是我们在.htaccess中用来处理类似问题的内容

DirectorySlash Off
DirectoryIndex index.php index.html index.htm

似乎没有做到这一点,我以后会记住这一点的。好吧,在URI的最后,斜杠没有被添加(我更新了我的帖子)。然而,斜杠仍然添加在#符号之前。糟糕,我再次更新了我的帖子,说我希望我的URL添加尾随斜杠,但只有当URL不以#@结尾时,你才能这样做,至少不能使用htaccess和mod#u rewrite。Apache不知道URL何时以“#”结尾。URI片段完全是为浏览器设计的。对我来说很有魅力。谢谢@JonLin!