简体   繁体   English

重写.htaccess中的规则

[英]Rewrite rule in .htaccess

I have already spent a lot of time to create a rewrite rule in .htaccess but without luck so far. 我已经花了很多时间在.htaccess中创建一个重写规则,但是到目前为止还算不上成功。 Is there any genius to help me please? 有什么天才可以帮助我吗?

I would like to rewrite this URL: 我想重写此URL:

http://www.domain.com/all-players/Spain-Real-Madrid.php?playername=Cristiano+Ronaldo+dos+Santos+Aveiro

into this one: 进入这个:

http://www.domain.com/Spain-Real-Madrid-Cristiano-Ronaldo-dos-Santos-Aveiro-player.html

Changes are: 更改为:

  1. remove the level /all-players 删除级别/所有玩家
  2. concat Spain-Real-Madrid with Cristiano+Ronaldo+dos+Santos+Aveiro (and add an hyphen in between) 用克里斯蒂亚诺+罗纳尔多+ dos +桑托斯+阿维罗(西班牙和马德里)并置西班牙(-马德里)
  3. convert + into - in the value of the parameter playername 在参数playername的值中将+转换为-
  4. add the word '-player' before the file extension 在文件扩展名之前添加单词“ -player”

Another example is to convert this path: 另一个示例是转换此路径:

/all-players/England-Arsenal.php?playername=Thierry+Henry

into: 变成:

/England-Arsenal-Thierry-Henry-player.html

Thanks heaps, 谢谢堆,

AFAIR, there's no way to do this in one Regexp. AFAIR,无法在一个Regexp中做到这一点。 Therefore, you'll need to get it in several steps. 因此,您需要分几个步骤来获取它。 Most probably the following will not work just copy-pasted, cause I'm not very experienced in rewrites. 以下内容很可能仅复制粘贴就无法使用,因为我对重写的经验不是很丰富。

RewriteRule /all-players/(.*?)\.php?playername=(.*) /all-players-1/$1-$2


RewriteCond %{THE_REQUEST} ^/all-players-1/
RewriteCond %{QUERY_STRING} \+
RewriteRule \+ -


RewriteCond %{THE_REQUEST} ^/all-players-1/
RewriteCond %{QUERY_STRING} ^[^+]+$
RewriteRule /all-players-1/(.*)  /$1-player.html [L]

Here is a recusrion based rule that will do what you need: 这是一个基于规则的规则,它将满足您的需求:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^playername=(.*)(&|$) [NC]
RewriteRule ^all-players/([^\.]+)\.php$ /$1-%1-player.html? [L,NC]

RewriteRule ^([^+]+)\+([^+]+)(\+.*)?$ $1-$2$3 [R,L]
RewriteRule ^([^+])\+ $1 [R,L]

This code will recursively replace all the + signs with hyphens - . 此代码将用连字符- 递归替换所有+号。

This will externally redirect : 这将在外部重定向

  • /all-players/England-Arsenal.php?playername=Thierry+Henry to /England-Arsenal-Thierry-Henry-player.html /all-players/England-Arsenal.php?playername=Thierry+Henry/England-Arsenal-Thierry-Henry-player.html
  • /Spain-Real-Madrid.php?playername=Cristiano+Ronaldo+dos+Santos+Aveiro to /Spain-Real-Madrid-Cristiano-Ronaldo-dos-Santos-Aveiro-player.html /Spain-Real-Madrid.php?playername=Cristiano+Ronaldo+dos+Santos+Aveiro /Spain-Real-Madrid-Cristiano-Ronaldo-dos-Santos-Aveiro-player.html /Spain-Real-Madrid.php?playername=Cristiano+Ronaldo+dos+Santos+Aveiro到/ /Spain-Real-Madrid-Cristiano-Ronaldo-dos-Santos-Aveiro-player.html /Spain-Real-Madrid.php?playername=Cristiano+Ronaldo+dos+Santos+Aveiro - /Spain-Real-Madrid-Cristiano-Ronaldo-dos-Santos-Aveiro-player.html /Spain-Real-Madrid.php?playername=Cristiano+Ronaldo+dos+Santos+Aveiro - /Spain-Real-Madrid-Cristiano-Ronaldo-dos-Santos-Aveiro-player.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM