htaccess - 重写除/上传以外的所有内容并使用密码保护

时间:2014-02-13 17:02:07

标签: apache .htaccess mod-rewrite

我有一个带有“index.php”和“upload.php”的文件夹。

我想将除“/ upload”之外的所有请求重定向到我的“index.php”:

1。)http://example.com/abc123 - >的index.php

2。)http://example.com/upload - > /upload.php

我想用密码保护/上传。到目前为止,我设法将所有内容重定向到我的index.php

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

在我的“index.php”中,我可以使用

trim($_SERVER['REQUEST_URI'], '/')

如何添加规则以重定向/上传到我的“upload.php”并使用密码保护文件?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用否定前瞻((?!...))执行此操作:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(?!/?upload).*$ index.php [NC,L]
RewriteRule ^/?upload$ upload.php [NC,L]

密码保护部分:

<FilesMatch "upload.php">
    # Change "Members Only" to whatever you want
    AuthName "Members Only"
    AuthType Basic
    # This should be a path to a .htpassword file; change it to match your system
    # You can generate a .htpassword file online in lots of places, like http://www.htaccesstools.com/htpasswd-generator/
    AuthUserFile /home/cpanelusername/.htpasswd
    require valid-user
</FilesMatch>

(根据OP的评论编辑)