【问题标题】:htaccess rewrite url namehtaccess 重写 url 名称
【发布时间】:2016-03-23 19:10:23
【问题描述】:

我已经为这个问题头疼了将近 2 个小时。

我有一个网站(我们称之为 example.com) 我需要重写这个网址:

example.com/plant.php?naam=Brahea%20Armata

example.com/Brahae_Armata

这就是我的 .htaccess 文件在 atm 中的样子:

RewriteEngine On
RewriteCond %{HTTP_HOST} example.com/plant.php?naam=$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

这行不通。有谁知道问题出在哪里?

【问题讨论】:

  • 网址里可以有多个%20吗?
  • 不,我认为我永远不会在一个 URL 中有两个空格。

标签: regex .htaccess mod-rewrite


【解决方案1】:

您可以在 root .htaccess 中使用这些规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} /plant\.php\?naam=(.*)\%20(\S*)\sHTTP [NC]
RewriteRule ^ /%1_%2? [NE,R=301,L]

RewriteCond %{THE_REQUEST} /plant\.php\?naam=(.*)\sHTTP [NC]
RewriteRule ^ /%1? [NE,R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) page.php?name=$1 [QSA,L]

【讨论】:

  • 某些页面的 URL 中不包含空格。使用以下代码是否正确? RewriteCond %{THE_REQUEST} /plant\.php\?naam=(.*)\sHTTP [NC] RewriteRule ^ /%1? [NE,R=301,L]
猜你喜欢
  • 2021-12-23
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 2017-02-04
  • 2021-06-29
  • 2021-11-02
相关资源
最近更新 更多