当目录存在时用htaccess重写url

2022-01-04 00:00:00 php .htaccess url-rewriting

我正在尝试将动态页面重定向到目录,例如:

I'm trying to redirect a dynamic page to a dir, example:

url.com/index.php?page=download
to
url.com/download

规则很简单:

RewriteRule ^download$ /index.php?page=download

完整的.HTACCESS

FULL .HTACCESS

RewriteEngine On
RewriteBase /

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

Redirect 301 /index.php?page=download http://url.com/download
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^download$ /index.php?page=download

问题是我已经有一个名为download"的目录,它返回一个403 Forbidden Error".我注意到将目录重命名为与规则使用的名称(下载)不同的任何名称都可以.

The problem is that I already have a directory named "download" and it's returning me a "403 Forbidden Error". I have noticed that renaming the directory to anything different than the name (download) the rule is using will work.

所以,我的问题是:如何按照我需要的方式重写我的 url 并保留目录?

So, my question is: how can I have my url rewritten the way I need and keep the directory?

-- 编辑 --

默认情况下,我的服务器使用403 Forbbiden 错误"保护每个目录.我相信这是它不起作用的原因,但我不确定.真的有冲突吗?发生了什么?

My server, by default, protects every directory with a "403 Forbbiden Error". I believe this is the reason why it's not working, but I'm not sure. Is it a conflict indeed? What is happening?

推荐答案

/download/?page=download

发生这种情况的原因是因为某处 mod_dir 将所有对缺少尾部斜杠的目录的请求重定向到包含尾部斜杠.这会干扰您的重写规则.由于您的服务器已自动设置为拒绝列出目录,因此继续关闭目录斜杠可能是安全的:

The reason why this is happening is because somewhere mod_dir redirects all requests for directories that are missing the trailing slash to include the trailing slash. This is interferring with your rewrite rule. Since your server is automatically setup to deny listing of directories, it's probably safe to go ahead and turn directory slashes off:

DirectorySlash Off

答案:https://stackoverflow.com/users/851273/jon-lin

相关文章