【问题标题】:htaccess for changing index.php?ms=user to /userhtaccess 用于将 index.php?ms=user 更改为 /user
【发布时间】:2014-04-09 06:55:16
【问题描述】:

我想用 301 代码重定向我的网站网址

http://www.domain.com/index.php?ms=user&ms_1=name   -->  http://www.domain.com/user/name
http://www.domain.com/index.php?ms=2652&ms_1=title  -->  http://www.domain.com/2652/title
http://www.domain.com/index.php?ms=questions        -->  http://www.domain.com/questions
http://www.domain.com/index.php?ms=aaa&ms_1=bbb&ms_2=ccc      -->  http://www.domain.com/aaa/bbb/ccc

ms后面的东西也可以有空格和utf8字符

我该怎么做?

【问题讨论】:

  • 你已经尝试了什么?
  • 很高兴知道你想要什么,但如果你能展示你解决它的尝试会更好。

标签: php .htaccess


【解决方案1】:

这应该可以工作

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?ms=(.*)&ms_1=(.*)&ms_2=(.*)\ HTTP
RewriteRule ^ /%2/%3/%4\? [R=301,L]
RewriteRule ^(.*)/(.*)/(.*)$ /index.php?ms=$1&ms_1=$2&ms_2=$3 [L]

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?ms=(.*)&ms_1=(.*)\ HTTP
RewriteRule ^ /%2/%3\? [R=301,L]
RewriteRule ^(.*)/(.*)$ /index.php?ms=$1&ms_1=$2 [L]

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?ms=(.*)\ HTTP
RewriteRule ^ /%2\? [R=301,L]
RewriteRule ^(.*)/$ /index.php?ms=$1 [L]

它会将http://www.domain.com/index.php?ms=aaa&ms_1=bbb&ms_2=ccc 更改为http://www.domain.com/aaa/bbb/ccc,但是对于您拥有的其他两个链接,您只能看到http://www.domain.com/index.php?ms=aaa&ms_1=bbb&ms_2=ccc 等的内容。

【讨论】:

    【解决方案2】:

    我相信你可以启用 Apache 重写模块并给出重写规则来做到这一点。它具有具有反向引用能力的正则表达式。 一个可能的(因为我对重写模块中的正则表达式不太熟悉)规则集可能是:

     ReWriteRule ^/index.php?ms=(.+) /$1
     ReWriteRule ^/index.php?ms=(.+)&ms_1=(.+) /$1/$2
     ReWriteRule ^/index.php?ms=(.+)&ms_1=(.+)&ms_2=(.+) /$1/$2/$3
    

    【讨论】:

    • 这将产生 404 错误,因为他只想隐藏他丑陋的 URL 并显示漂亮的 URL,但使用丑陋的 URL 的内容。
    • 哦!我懂了。然后忽略我的回答。
    【解决方案3】:

    请按照代码进行操作,希望对您有所帮助。

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?ms=$1 [L]
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?ms=$1&ms_1=$2 [L]
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?ms=$1&ms_1=$2&ms_2=$3 [L]
    

    在此代码启用 apache 重写模块之前

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 2017-07-06
      • 2013-03-25
      相关资源
      最近更新 更多