【问题标题】:Redirect URL non existent subfolder to root将 URL 不存在的子文件夹重定向到根目录
【发布时间】:2013-06-15 21:16:16
【问题描述】:

我已将我的网站从 www.site.com/shop/ 子文件夹移至根目录。现在“shop”文件夹不再存在,我想将仍然访问 site.com/shop/whateverhtml 的人重定向到 site.com/whateverhtml 我尝试了不同的重写规则,但没有运气。

RewriteEngine on
RewriteRule %{REQUEST_URI} ^/shop/.*
RewriteRule shop/(.*)$ /$1 [R=301,L]

或者像这样

RedirectMatch 301 ^/shop/$ http://site.com/

谢谢。

【问题讨论】:

    标签: apache redirect rewrite


    【解决方案1】:
    RewriteCond %{REQUEST_URI} ^/shop(/.*)?$
    RewriteRule ^(.*)$ %1 [R=301,L]
    

    RewriteRule ^shop(/.*)?$ $1 [R=301,L]
    

    【讨论】:

    • 以上规则均无效。从谷歌的缓存链接(链接中带有“商店”文件夹)转到实际链接时,我仍然找不到 404 页面
    • 他们两个都由我工作......也许你有多视图,或者另一个 htaccess 覆盖了这些,我不知道......
    • 我注意到如果我在链接中添加子文件夹,重写规则就会起作用。这意味着这是因为谷歌在链接中缓存了带有“shop”文件夹的站点并更改了链接的名称。我想我必须等待 Google 重新缓存该网站...
    【解决方案2】:

    重定向

    这需要您创建一个空的shop 文件夹。 在该文件夹中,创建一个名为 index.html 的 HTML 文件。访问http://site.com/shop 时,用户将被引导至此处。 在该文件中,输入以下内容:

    <html>
    <head>
    <title>Redirecting...</title>
    <meta http-equiv="refresh" content="0; url=http://site.com/">
    </head>
    <body>
    You are being redirected...
    </body>
    </html>
    

    这会将他们重定向到http://site.com/

    取景

    您可以使用&lt;iframe&gt; 在页面上显示另一个页面。 &lt;iframe&gt; 显示同一页面内的另一个页面,无需重定向。您仍然必须保留商店文件夹。在其中放入 index.html 的内容:

    <html>
    <head>
    <title>Shop</title>
    </head>
    <body>
    <iframe src="http://site.com/"></iframe>
    </body>
    </html>
    

    这将创建一个带有 site.com 的 iframe 的页面

    【讨论】:

    • 我试图保持 uri 不变,而不仅仅是将它们重定向到新站点。像 site.com/shop/folder1/page.html 到 site.com/folder1/page.html
    • 在这种情况下,您可以使用&lt;iframe&gt;
    猜你喜欢
    • 1970-01-01
    • 2020-01-08
    • 2012-10-23
    • 2017-01-29
    • 2021-09-03
    • 2011-01-19
    • 2017-02-01
    • 2011-01-14
    相关资源
    最近更新 更多