Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
998 views
in Technique[技术] by (71.8m points)

php - Rewriting url with htaccess when a directory exists

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

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

The rule is very simple:

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

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

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.

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

-- EDIT --

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
/download/?page=download

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

Answer by: https://stackoverflow.com/users/851273/jon-lin


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...