Rewriting url with htaccess when a directory exists(当目录存在时用htaccess重写url)
问题描述
我正在尝试将动态页面重定向到目录,例如:
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
这篇关于当目录存在时用htaccess重写url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当目录存在时用htaccess重写url
基础教程推荐
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- HTTP 与 FTP 上传 2021-01-01