.htaccess RewriteRule从download.php?token = something到/ download / something

时间:2013-08-23 11:47:40

标签: regex apache .htaccess mod-rewrite

我有这个网址:
http://www.example.com/subf/subf2/download.php?token=6309838552568
可以将此网址重写为此网址吗? http://www.example.com/subf/subf2/download/6309838552568

注意:如果有人输入这样的网址: http://www.example.com/subf/subf2/download/6309838552568/sbuf3/subf4
我想向他们展示一个错误页面。

以下答案

提示:包含JS和CSS等文件需要在绝对路径中引用。

1 个答案:

答案 0 :(得分:0)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT/subf/subf2/中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subf/subf2/

RewriteRule ^(download)/(\w+)/?$ $1.php?token=$2 [L,QSA,NC]

# to take care of css, js, images
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.php$
RewriteRule ^download/(.+)$ /subf/subf2/$1 [L,R=301,NC]