【问题标题】:Apache .htaccess rewrite rule to redirect urls from sub-directory to parent directoryApache .htaccess 重写规则将 url 从子目录重定向到父目录
【发布时间】:2015-08-04 14:46:18
【问题描述】:

我正在寻找一个 Apache .htaccess 重写规则来将 url 从子目录重定向到父目录。

以下是用例:

网址重定向:

http://localhost/content/videos redirects (302) to http://localhost/videos
http://localhost/content/article/some-awesome-article redirects (302) to http://localhost/article/some-awesome-article
http://localhost/content/some-html-file.html redirects (302) to http://localhost/some-html-file.html
etc.

虽然以下不应该:

http://localhost/content/some-image.jpg
http://localhost/content/admin (Specific)

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以在DOCUMENT_ROOT/.htaccess 文件中使用此代码:

    RewriteEngine On
    
    RewriteRule ^content/((?!admin|some-image\.jpg).+)$ /$1 [L,NC,R=302]
    

    编辑:使用RedirectMatch

    RedirectMatch 302 ^/content/((?!(admin|(.*\.(gif|jpeg|jpg|png|css|js|woff|ttf|svg|eot|GIF|JPEG|JPG‌​|PNG|CSS|JS|WOFF|TTF|SVG|EOT)))).+)$ /$1 
    

    【讨论】:

    • 这条规则之前有规则吗?这个 .htaccess 在哪里?
    • 我用这个:RedirectMatch 302 ^/content/((?!(admin|(.*\.(gif|jpeg|jpg|png|css|js|woff|ttf|svg|eot |GIF|JPEG|JPG|PNG|CSS|JS|WOFF|TTF|SVG|EOT)))).+)$ /$1 成功了。
    • 好的,这类似于我在回答中建议的否定正则表达式规则模式。
    猜你喜欢
    • 2020-08-23
    • 2014-07-14
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2019-01-27
    • 2016-05-05
    • 1970-01-01
    相关资源
    最近更新 更多