【问题标题】:Removing an element anywhere in URL by htaccess通过 htaccess 删除 URL 中任意位置的元素
【发布时间】:2015-12-19 20:39:31
【问题描述】:

首先:我过去有一个错误的策略,在我的 url 中包含无用/无意义的“seo_url=keyword”元素,现在我想在 URL 中的任何位置删除它,并在没有该元素的情况下重写我的 URL。 (301 重定向)

第二:有时一个元素或某些元素是空的,例如“location=”或“location=&select=”,如果它们在 URL 中的任何位置为空,我会删除这些元素,并在没有这些元素的情况下重写我的 URL。 (301 重定向)

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]


RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC,OR]
RewriteCond %{REQUEST_FILENAME} -l [NC]
RewriteRule .* - [L]



RewriteRule (.*)/$ /?countrySelect=$1
RewriteRule (.*)/+/(.*)-(.*)\.html$  search-products.php?countrySelect=$1&productselect=$2&submit=1
RewriteRule (.*)/tag/(.*) search-products.php?countrySelect=$1&keyword=$2
RewriteRule (.*)/(.*)/(.*)-(.*)\.html$  search-products.php?productLocation=$2&countrySelect=$1&productselect=$3&submit=1
RewriteRule (.*)/(.*)_(.*)\.html$  detail.php?name=$2&id=$3&detail=true&countrySelect=$1</IfModule>

【问题讨论】:

  • 请显示您当前使用的代码。
  • 按要求添加代码
  • 您仅在内部使用这些链接(用于公开使用漂亮链接)。我不明白您为什么要对更正的链接进行重定向?
  • 不,我公开使用它们。我的代码中有什么让你这么想吗?
  • 你从漂亮的链接重写...

标签: php .htaccess mod-rewrite url-rewriting


【解决方案1】:

Ad 1) 去除查询字符串中的seo_url=keyword,使用RewriteCond提取前后部分保留

RewriteCond &%{QUERY_STRING}& (.*?)&seo_url=.*?&(.*)
RewriteRule ^ %{REQUEST_URI}?%1&%2 [R,L]

Ad 2) 从查询字符串中删除所有空元素的效果相同

RewriteCond &%{QUERY_STRING}& (.*?)&.*?=&(.*)
RewriteRule ^ %{REQUEST_URI}?%1&%2 [L]

您必须将查询字符串添加到目标 URL。如果没有,查询字符串将被原封不动地追加。

【讨论】:

  • 你能举个例子什么意思吗?
  • 也许我误解了你的问题。我想,你想删除部分查询字符串,不是吗?
  • 我想删除所有这些:"seo_url=AnyKeywordHere" URL 中的任何位置
  • 还有所有空元素/参数
  • 删除seo_url 是通过第一条规则完成的。使用第二条规则删除空参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多