htaccess将所有未找到的404重定向到特定的URL

时间:2019-03-06 08:23:38

标签: apache .htaccess mod-rewrite

我需要将所有404 not found重定向到更符合用户体验的特定URL。

此重定向到特定的index.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

如何将所有404未找到的页面而不是index.php重定向到特定的URL,例如:https://www.example.com/jobs

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

ErrorDocument 404 http://example.com/404/ 
ErrorDocument 500 http://example.com/500/ 
# or map them to one error document: 
# ErrorDocument 404 /pages/errors/error_redirect.php 
# ErrorDocument 500 /pages/errors/error_redirect.php 
RewriteEngine On 
RewriteBase / 
RewriteCond %{REQUEST_URI} ^/404/$ 
RewriteRule ^(.*)$ /pages/errors/404.php [L] 
RewriteCond %{REQUEST_URI} ^/500/$ 
RewriteRule ^(.*)$ /pages/errors/500.php [L] 
# or map them to one error document: 
#RewriteCond %{REQUEST_URI} ^/404/$ [OR] 
#RewriteCond %{REQUEST_URI} ^/500/$ 
#RewriteRule ^(.*)$ /pages/errors/error_redirect

希望这会有所帮助:)