【问题标题】:Deleting a directory from the path to the URL using .htaccess使用 .htaccess 从 URL 的路径中删除目录
【发布时间】:2017-08-07 14:00:24
【问题描述】:

有一个类似的链接

domain.com/dir/page

它显示页面page.php的内容,该页面位于站点的dir目录中。

当前视图 .htaccess 位于站点根目录:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule .* http://%1/$0 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php/
RewriteCond %{HTTPS} =off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]
ErrorDocument 404 /404.php

如何将 domain.com/dir/page 的 URL 替换为 domain.com/page(即删除 dir从路径)?

附:据了解,该解决方案应适用于指定类型的所有 URL。

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    尝试在您的 .htaccess 文件中使用它,确保它高于您拥有的任何其他规则:

    RewriteEngine On
    RewriteRule ^dir/(.*)$ /$1 [L,R=301]
    

    在测试之前清除缓存。

    【讨论】:

    • 谢谢。你完美地回答了这个问题。抱歉,原提问者没有为此提供道具。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多