【问题标题】:How to change multiple parameters in subfolder如何更改子文件夹中的多个参数
【发布时间】:2023-03-22 05:31:01
【问题描述】:

如果查询字符串有多个参数,如何重定向和更改子文件夹中的最后一个。如果只有一个参数删除子文件夹?我只能重定向一个,但是当我尝试两个参数时,它变得一团糟。

localhost/mynews/category.php?cat=news
localhost/mynews/category.php?cat=news&subcat=9

localhost/mynews/news
localhost/mynews/news/9

【问题讨论】:

    标签: php .htaccess redirect url-rewriting query-string


    【解决方案1】:

    您可以在mynews/.htaccess使用此代码:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /mynews/
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /category\.php\?cat=([\w-]+)&subcat=([\w-]+)\s [NC]
    RewriteRule ^ %1/%2? [R=301,L]
    
    RewriteCond %{THE_REQUEST} /category\.php\?cat=([\w-]+)\s [NC]
    RewriteRule ^ %1? [R=301,L]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^([\w-]+)/([\w-]+)/?$ category.php?cat=$1&subcat=$2 [L,QSA,NC]
    
    RewriteRule ^([\w-]+)/?$ category.php?cat=$1 [L,QSA,NC]
    

    【讨论】:

    • 感谢上帝你来了,在测试不同的方式和答案时 url 无法重定向,现在 URL 被你的答案重定向但是为什么我面对找不到对象的页面
    • localhost/mynews/news/9 直接在浏览器中工作吗?
    • 不,只有 localhost/mynews/category.php?cat=news&subcat=9 有效 localhost/mynews/news/9 无效
    • RewriteRule ^([a-z0-9\-]+)/?$ category.php?cat=$1 [NC,L] RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ category.php?cat=$1&subcat=$2 [NC,L] URl 使用此规则重写,但我无法使用此重定向
    • 删除^mynews/ 并使用RewriteBase /mynews/ 终于对我有用。非常感谢。
    【解决方案2】:

    如果你想这样做,在 .htaccess 中你可以做下一个:

    Rewriterule ^mynews/(.+)/(.+)$ category.php?cat=$1&subcat=$2
    

    (.+) 代表$ 之后的每个值。

    【讨论】:

    • 谢谢,但没有任何改变,仍然是相同的网址
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2015-03-21
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    相关资源
    最近更新 更多