.htaccess - mod-rewrite和重定向配置文件URL以及参数来清理它们

时间:2016-11-08 14:57:23

标签: apache .htaccess mod-rewrite url-redirection friendly-url

我有以下用户个人资料的链接:

-

我的目标是对这些URL进行mod重写,使它们看起来更好,即。他们应该看起来像:

https://example.com/chat/index.php?action=user&member=547
https://example.com/chat/index.php?action=user&member=11540

没有尾部斜杠的网址,即

https://example.com/chat/member-547/
https://example.com/chat/member-11540/

应该301转发给一个斜杠,即

https://example.com/chat/member-547

所以在.htaccess中我尝试了这个,但只有第一行似乎有用而且还没有完成:

https://example.com/chat/member-547/

TO SUMMARIZE:

  1. 当有人输入以下网址时:

    RewriteRule ^chat/member-([0-9]+)/$ ./index.php?action=user&member=$1
    RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
    

    它应该被301重定向到:

    https://example.com/chat/index.php?action=user&member=547
    
  2. 当有人进入时:

    https://example.com/chat/member-547/
    

    它也应该被301重定向到:

    https://example.com/chat/member-547
    
  3. 我希望有一种有效的方法可以做到。

2 个答案:

答案 0 :(得分:2)

您可以使用以下规则:

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^action=([^&]+)&member=(.+)$
RewriteRule /chat/index\.php$ /chat/member-%2/? [L,R=301]
RewriteRule ^chat/member-(.+)/?$ /chat/index.php?action=user&member=$1 [L]

答案 1 :(得分:2)

Starkeen的回答是正确的,并且会有效。我只是建议一个替代方案,它对原始请求变量本身起作用:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /chat/index\.php\?action=user&(member)=(\d+) [NC]
RewriteRule ^chat/index\.php$ /chat/%1-%2/? [R=301,L]

RewriteRule ^chat/member-([0-9]+)$ /member-$1/ [R=301,L]
RewriteRule ^chat/(member)-(\d+)/$ /chat/index.php?action=user&$1=$2 [L]