【问题标题】:htaccess - rewrite URL ending with specific string and capturing the unique parthtaccess - 重写以特定字符串结尾的 URL 并捕获唯一部分
【发布时间】:2021-02-24 08:12:12
【问题描述】:

我想将几个 URL 输入到一个 php 文件中,该文件将处理页面的内容,这些 URL 就像

domain.com/fashion-registration
domain.com/singing-registration

我想捕获以 -registration 结尾的 URL 并将 fashionsinging 输入页面,但它似乎不起作用。这是我尝试过的

RewriteRule ^(.*)$-registration category.php?link=$1 [NC,L,QSA]

【问题讨论】:

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


    【解决方案1】:

    请您尝试关注一下。

    RewriteEngine ON
    RewriteCond %{REQUEST_URI} ^/(.*)-registration/?$ [NC]
    RewriteRule ^(.*)$ /category.php?link=%1 [NE,NC,L]
    

    或者你也可以尝试跟随,没有 RewriteCond。

    RewriteEngine ON
    RewriteRule ^(.*)-registration/?$ /category.php?link=$1 [NE,NC,L]
    

    OP 尝试中的问题: 由于您使用了 ^(.*)$,之后您在正则表达式中使用了 -registration,这就是您的正则表达式永远不会匹配的原因-registration

    【讨论】:

    • @VeeK,我在发布之前对其进行了测试,它对我来说效果很好,看到这个curl -IL "http://localhost:80/fashion-registration" -----> Location: http://localhost/category.php?link=fashion,它对我有用,让我知道。跨度>
    • 我的站点不在本地主机的根目录中。我用RewriteBase 修复了它。如果可以的话,还有另一个问题。如果 URL 中包含 -,则它不起作用。例如,/fashion-show-registration。你能解决这个问题吗?
    猜你喜欢
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    相关资源
    最近更新 更多