我有一组链接,例如:
samplefile.php?post=Test 1
samplefile.php?post=Test 2
...
samplefile.php?post=Test 14
...
我想分别重写或重定向到:(我需要两个不同的选项)
samplefile-Test-1.php and (samplefileTest1.php)
samplefile-Test2.php and (samplefileTest2.php)
...
samplefile-Test-14.php and (samplefileTest14.php)
...
因此需要用' - '替换空白区域,或者根本修剪空白区域。 有可能实现我想要的吗? 如果是,我该怎么做? 非常感谢您的帮助!
更新:我可以做到这一点,但这还不够好,希望做得更好(:
RewriteCond %{QUERY_STRING} ^post=(.*)$
RewriteRule ^samplefile\.php$ http://www.exampledomain.com/? [R=301,L]
答案 0 :(得分:1)
您可以使用这两组不同的规则:
samplefile-Test-1.php, samplefile-Test-2.php .. samplefile-Test-3.php
# first strip query string and redirect /samplefile.php?post=Test 1 to
# /samplefile-Test 1.php
RewriteCond %{QUERY_STRING} ^post=([^&]+) [NC]
RewriteRule ^(samplefile)\.php/?$ /$1-%1.php? [NC,L,NE,R=302]
# if URL only has white-spaces in the middle then redirect to
# /testfile-Test-1.php
RewriteRule ^samplefile(-\S*)\s+(\S*\.php)$ /testfile$1-$2 [L,NE,R=302]
# if URL has white spaces at multiple places then replace whitespace by hyphen
# and loop the rule
RewriteRule ^(samplefile-\S*)\s+(\S+\s+.*\.php)$ /$1-$2 [N,NC,DPI]
samplefileTest1.php, samplefileTest2.php .. samplefileTest14.php
RewriteCond %{QUERY_STRING} ^post=([^&]+) [NC]
RewriteRule ^(samplefile)\.php/?$ /$1%1.php? [NC,L,NE,R]
RewriteRule ^samplefile(\S*)\s+(\S*\.php)$ /testfile$1$2 [L,NE,NC,R=302]
RewriteRule ^(samplefile\S*)\s+(\S+\s+.*\.php)$ /$1$2 [N,NC,DPI]