【问题标题】:htaccess mod_rewrite - Trailing Slash and loop of redirectshtaccess mod_rewrite - 尾部斜线和重定向循环
【发布时间】:2016-06-06 18:03:09
【问题描述】:

这是我的 htaccess 代码:

重写引擎开启
RewriteRule ^s/(.*)/(.*) /index.php?search=$1&category=$2 [L,QSA]

RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
重写规则 ^(.*)$ /s/%1/? [R=301,L]

RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
重写规则 ^(.*)$ /s/%1/%2/? [R=301,L]

我需要制定一个这样的重写规则:

http://mywebsite.com/s/query+term => http://mywebsite.com/?search=query+term

或者如果有一个类别

http://mywebsite.com/s/query+term/Category => http://mywebsite.com/?search=query+term&category=Category

我还需要将所有旧网址重定向到新网址。

有了这个规则,我所能得到的就是将搜索词和类别名称连接在一起。简而言之,就好像我一直:
@987654321@
如果我删除所有条件(RewriteCond),只留下重写规则并添加一个斜杠:

重写引擎开启
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]

以直接的方式访问我感兴趣的网址,整个过程都有效。但我不会有重定向..

所以我尝试设置这样的规则:

重写引擎开启
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]

RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
重写规则 ^(.*)$ /s/%1/? [R=301,L]

RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
重写规则 ^(.*)$ /s/%1/%2/? [R=301,L]

但是有了这条规则,我得到的只是一个重定向循环。

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    您可以使用以下规则:

    RewriteEngine on
    
    #1--Redirect from "?search=foobar&cat=cat" to "/s/foobar/cat" --#
    RewriteCond %{THE_REQUEST} /\?search=([^&]+)&cat=([^\s]+) [NC]
    RewriteRule ^ /s/%1/%/%2? [NC,L,R]
    #2--Redirect from "/?search=foobar" to "/s/foobar" --#
    RewriteCond %{THE_REQUEST} /\?search=([^/\s]+) [NC]
    RewriteRule ^ /s/%1? [NC,L,R]
    #1--Rewrite "s/foobar/cat" to "/?search=foobar&cat=cat"--#
    RewriteRule ^s/([^/]+)/([^/]+)/?$ /?search=$1&$category=$2 [NC,L]
    #2--Rewrite "s/foobar" to "/?search=foobar"--#
    RewriteRule ^s/([^/]+)/?$ /?search=$1 [NC,L]
    

    【讨论】:

    • 完美,我已经对您的代码进行了一些调整pastebin.com/stj6Su0Z 并且工作正常。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多