[英]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: 更改为:
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.