【问题标题】:Rewriting URL that contains multiple parameters with multi-word strings (with mod_rewrite)使用多字字符串重写包含多个参数的 URL(使用 mod_rewrite)
【发布时间】:2013-06-23 23:22:42
【问题描述】:

试图重写具有多个参数且其中一个参数包含多个单词(提交时以空格分隔)的页面的 URL。

这将是 URL:www.ABCD.com/store/itemsDescr.php?categ=headbands&id=123&name=brown%20with%20black

我希望 URL 显示如下: www.ABCD.com/store/headbands/123/brown-with-black

我试过了,但没用:

RewriteCond %{QUERY_STRING} ^categ=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &categ=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php$ $0/%1

RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &id=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php/[^/]+$ $0/%1

RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &name=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php/([^/]+/[^/]+)$ http://www.ABCD.com/store/$1/%1/? [L,QSA,NC]

任何帮助将不胜感激!

注意:在我要实施这条规则之前,我已经为另一个页面准备了这条规则,以防它产生冲突:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(store)/index\.php\?categ=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteRule ^store/(.+?)/?$ /store/index.php?categ=$1 [L,QSA,NC]

【问题讨论】:

  • But I don't want to redirect. 是什么意思?
  • 这实际上是一个错字。删除那个。我只是希望 URL 看起来对 SEO 更友好...
  • 好吧,我会采取行动。 (涉及更多)
  • 好的,谢谢。我认为我上面的代码对于我需要的东西来说太复杂了,那就是用 3 个参数重写......

标签: .htaccess url redirect mod-rewrite rewrite


【解决方案1】:

通过httpd.conf启用mod_rewrite和.htaccess,然后把这段代码放到你.htaccessDOCUMENT_ROOT目录下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^categ=([^&]+)&id=([^&]+)&name=([^&]+)$ [NC]
RewriteRule ^(store)/itemsDescr\.php$ /$1/%1/%2/%3? [R=302,L,NC,NE]

RewriteRule ^(store)/([^\s]+)\s([^\s]+)$ /$1/$2-$3 [R=302,L,NC]

RewriteRule ^(store)/([^\s]+)\s(.+)$ /$1/$2-$3 [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(store)/([^/]+)/([^/]+)/ /$1/itemsDescr.php?categ=$2&id=$3 [L,QSA,NC,NE]

【讨论】:

  • mm...它不再加载预期的页面,也没有将 URL 更改为好看的页面。
  • 这是一个经过测试的解决方案。你试过这个网址:http://www.ABCD.com/store/itemsDescr.php?categ=headbands&id=123&name=brown%20with%20black 还是别的什么?
  • 是的,这就是 URL。它的作用是:它保留原始的丑陋 URL,并加载页面 www.ABCD.com/store/ 而不是 itemsDescr.php 一个
  • 是的,首先注释掉所有预先存在的东西并仅使用上述规则进行测试,看看 URL 是否根据您的要求更改。
  • 这是好的第一步。一旦我们在浏览器中有好的 URL,加载内容就不难了。
猜你喜欢
  • 2013-04-05
  • 2012-10-22
  • 2012-09-23
  • 2010-11-28
  • 2017-05-18
  • 1970-01-01
  • 2016-07-29
  • 2012-06-25
  • 2016-08-13
相关资源
最近更新 更多