为什么我的.htaccess重定向不起作用?

时间:2014-06-18 07:55:55

标签: regex apache .htaccess mod-rewrite rewrite

我正在尝试重定向服务器中不存在的旧pdf的传入请求。这就是我在我的.htaccess中所做的,但是它没有用。它总是带我到自定义404错误页面。我做错了什么?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]

RewriteRule ^files\/2002\/pdfs\/Home\%20Doc\.pdf$ "http\:\/\/mydomain\.com\/docs\/" [R=301,L]

此处files/2002/pdfs/Home Doc.pdf是旧文件,不再存在于网站中。

1 个答案:

答案 0 :(得分:0)

重新排列规则,以便在静默重写之前保持301重定向:

RewriteEngine On
RewriteBase /

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

RewriteRule "^files/2002/pdfs/Home Doc\.pdf$" http://mydomain.com/docs/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]