【问题标题】:RewriteRule for htaccess using regex使用正则表达式为 htaccess 重写规则
【发布时间】:2013-05-02 07:31:54
【问题描述】:

我需要将 /start 重定向到 /start/index.html 并将其他所有内容 (/anything) 重定向到 /index.html

什么是正确的正则表达式重写?

谢谢

【问题讨论】:

    标签: regex .htaccess


    【解决方案1】:

    我需要将 /start 重定向到 /start/index.html 并将其他所有内容 (/anything) 重定向到 /index.html。什么是正确的正则表达式重写?

    您可以在根目录的一个 .htaccess 文件中尝试此操作:

    Options +FollowSymlinks -MultiViews
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI}  !index\.html      [NC]
    RewriteRule ^start/(.*)  /start/index.html?$1 [L,NC,QSA]
    

    在规则中,anything 作为查询传递给 index.php,并且 QSA 标志在存在时添加传入的标志。

    要将 anything 作为路径段传递,请将 ?$1 替换为 /$1 并删除 QSA 标志。它没用,因为任何传入的查询都会自动附加。

    对于永久重定向,将 [L,NC,QSA] 替换为 [R=301,L,NC,QSA]

    【讨论】:

      【解决方案2】:

      需要重写条件:

      RewriteRule ^start/?$ /start/index.html [R]
      
      RewriteCond %{REQUEST_URI} !^/start/?$
      RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.html [R]
      

      请考虑有关[R] 标志的链接:https://stackoverflow.com/a/15999177/2007055

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        • 2016-02-10
        • 1970-01-01
        • 2020-06-10
        • 2021-10-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多