【问题标题】:How to rewrite URL and return `404` for original links?如何重写 URL 并为原始链接返回“404”?
【发布时间】:2018-02-16 09:14:35
【问题描述】:

我在htdocs 文件夹中创建了我的网站,其中包含一些文件,例如:.htaccessweb.php ... 过去,我为我的网站使用了旧的 URL 结构:old.php?id=12。然后,我使用了一个新的:new/12,通过使用这个简单的.htaccess

RewriteEngine On
RewriteRule "^new/([0-9]+)/?$" "/old.php?$1"

现在,我不希望任何用户使用旧 URL 结构访问我的网站。你能告诉我:如何重写 URL 并为原始链接返回 404

【问题讨论】:

    标签: php apache .htaccess web url-rewriting


    【解决方案1】:

    最好的解决方案,尤其是对于 SEO,是做一个重定向 301:

    RewriteEngine on
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} \s/+old\.php\?id=(\d+) [NC]
    RewriteRule ^ /new/%1? [R=301,L,NE]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^new/(\d+)/?$ /old.php?id=$1 [L,QSA,NC]
    

    要轻松获得真正的 404,最简单的方法是将隐藏页面名称更改为 new.php(也在 htaccess 中)并删除 old.php 页面。

    【讨论】:

      【解决方案2】:

      试试这个:

      Options +FollowSymLinks
      RewriteEngine On
      
      RewriteCond %{SCRIPT_FILENAME} !-d
      RewriteCond %{SCRIPT_FILENAME} !-f
      
      RewriteRule ^new/(\d+)*$ ./old.php?id=$1
      

      【讨论】:

      • 朋友干杯!很高兴帮助:-)
      • 但它不会按照要求做,没有 404 也没有阻止旧网址
      猜你喜欢
      • 1970-01-01
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 2020-09-22
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      相关资源
      最近更新 更多