htaccess mod重写,即使文件夹存在 [英] htaccess mod rewrite even if folder exists

查看:59
本文介绍了htaccess mod重写,即使文件夹存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a website and I just figured out that I have this problem.

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

When users go to www.website.com/something I want htaccess to open www.website.com/index.php?s=something , this works fine with my current redirect except if I have that "something" as existing folder.

"s"代表子页面"

RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?s=$1 [L]

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

I have problems only if the folder exists ( /js /css /images ..etc ) so when I try to access www.website.com/images it still displays the correct page BUT it shows this in the address bar www.website.com/images/?s=images . One thing to note is that if I change the ?s=images to ?s=images213123 the php still only recognizes the /images/ as $_GET['s'];

我对htaccess并没有经验,但是到目前为止,这是我所做的事情,也许问题出在其他地方而不是上面的一行. 我尝试将[L]移到第三条规则,尝试将有问题的规则与其余规则分开,并在我有限的知识下尝试了一切,但我什么都做不到.

I'm not experienced with htaccess, but this is what I put together so far, maybe the problem is somewhere else and not in the line above. I tried moving the [L] to the 3rd rule, tried separating the problematic rule from the rest, tried everything I could with my limited knowledge, I couldn't get anything working.

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

Any hints or ideas are very appreciated! Thank you in advance

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,可以很好地达到我的目的.

Adding "DirectorySlash Off" and changing the conditions and rules as in @Jon Lin's answer fixed the issue. I also had to clear the Firefox cache to see the changes. This is my final htaccess that works fine for my purpose.

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条规则.那一个条件仅适用于第一个规则,而其他三个则没有条件与之相关.您需要确保所有4条规则都不会应用于对现有文件或目录的请求:

Rewrite conditions only get applied to the immediately following rule, so you have 1 condition and 4 rules. That 1 condition is only applied to the first rule, and the other 3 have no conditions tied to them. You need to make sure all 4 of your rules won't get applied to requests for existing files or directories:

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]

这篇关于htaccess mod重写,即使文件夹存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆