.htaccess htaccess mod rewrite即使文件夹存在

.htaccess htaccess mod rewrite即使文件夹存在,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我正在建立一个网站,我刚刚发现我有这个问题 当用户转到www.website.com/something我希望htaccess打开www.website.com/index.php?s=something时,这对我当前的重定向很有效,除非我将“something”作为现有文件夹 “s”代表“子页面” 只有当文件夹存在(/js/css/images..etc)时,我才会遇到问题,因此当我尝试访问www.website.com/images时,它仍会显示正确的页面,但会在地址栏中显示此页面www.w

我正在建立一个网站,我刚刚发现我有这个问题

当用户转到www.website.com/something我希望htaccess打开www.website.com/index.php?s=something时,这对我当前的重定向很有效,除非我将“something”作为现有文件夹

“s”代表“子页面”

只有当文件夹存在(/js/css/images..etc)时,我才会遇到问题,因此当我尝试访问www.website.com/images时,它仍会显示正确的页面,但会在地址栏中显示此页面www.website.com/images/?s=images。需要注意的一点是,如果我将?s=images更改为?s=images21323,php仍然只将/images/识别为$_GET['s']

我对htaccess没有经验,但这是我到目前为止总结出来的,也许问题出在其他地方,而不是上面这一行。 我试着把[L]移到第三条规则,试着把有问题的规则和其他规则分开,试着用我有限的知识做我能做的一切,我什么都做不到

任何提示或想法都非常感谢! 先谢谢你

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


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^u/([a-zA-Z0-9_-]+)$ /u.php?u=$1
RewriteRule ^p/([a-zA-Z0-9_-]+)$ /$1.php
RewriteRule ^([a-zA-Z0-9_-]+)/userpage/([a-zA-Z0-9_-]+)/?$ /c.php?s=$1&c=$2
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?s=$1 [L]


ErrorDocument 400 /index.php
ErrorDocument 401 /index.php
ErrorDocument 403 /index.php
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php
编辑:添加“DirectorySlash Off”并更改@Jon Lin的答案中的条件和规则,解决了这个问题。我还必须清除Firefox缓存以查看更改。这是我最后的htaccess,对我的目的来说效果很好

Options +FollowSymlinks
RewriteEngine On
DirectorySlash Off
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/([a-zA-Z0-9_-]+)$ /u.php?u=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([a-zA-Z0-9_-]+)$ /$1.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/userpage/([a-zA-Z0-9_-]+)/?$ /c.php?s=$1&c=$2

# I intentionally skipped the conditions here
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?s=$1 [L]


ErrorDocument 400 /index.php
ErrorDocument 401 /index.php
ErrorDocument 403 /index.php
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php

重写条件只应用于紧跟其后的规则,因此有1个条件和4个规则。这1个条件仅适用于第一个规则,而其他3个条件与它们无关。您需要确保所有4条规则不会应用于对现有文件或目录的请求:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/([a-zA-Z0-9_-]+)$ /u.php?u=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([a-zA-Z0-9_-]+)$ /$1.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/<userpage>/([a-zA-Z0-9_-]+)/?$ /c.php?s=$1&c=$2

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?s=$1 [L]
RewriteCond%{REQUEST\u FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^u/([a-zA-Z0-9_-]+)$/u.php?u=$1
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^p/([a-zA-Z0-9_-]+)$/$1.php
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$/c.php?s=$1&c=$2
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^([a-zA-Z0-9_-]+)/?$/index.php?s=$1[L]

@CristianBenescu这是因为mod_dir。Apache将始终重定向对缺少尾部斜杠的目录的请求,如果要关闭此选项,可以添加
DirectorySlash off
,但这样做可以使人们即使有索引文件也可以查看目录的内容
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/([a-zA-Z0-9_-]+)$ /u.php?u=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([a-zA-Z0-9_-]+)$ /$1.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/<userpage>/([a-zA-Z0-9_-]+)/?$ /c.php?s=$1&c=$2

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?s=$1 [L]