htaccess重写原始URL


htaccess to rewrite the original URL

我正在为我的网站创建SEO友好的URL。我有一个文件notfound.php,我正在将所有URL重定向到该文件。现在,我希望notfound.php在数据库中搜索URL并生成相应的内容。

但问题是它显示的URL是http://some_url/notfound.php我不希望此URL显示在地址栏中,而是希望使用原始URL。

例如,

http://some_url/hello/world正在使用ErrorDocument 404重定向到http://some_url/notfound.phpnotfound.php正在提供内容

但是如何在URL中显示http://some_url/hello/world而不是notfound.php?而且,在匹配数据库中的URL时,这是重定向和重写的正确方法吗?

您必须启用mod_rewrite,然后您可以尝试:

RewriteEngine on    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ notfound.php [L]

前两行确保您仍然可以加载现有的东西(如您可能需要的js和css文件)

ErrorDocument在您为其提供完整的URL时进行外部重定向。文档告诉您如何进行内部重定向。

http://httpd.apache.org/docs/current/custom-error.html#configuration