Reputation: 561
I have set of links like for example:
samplefile.php?post=Test 1
samplefile.php?post=Test 2
...
samplefile.php?post=Test 14
...
I would like to rewrite or redirect it respectively to: (I need both options for 2 separate cases)
samplefile-Test-1.php and (samplefileTest1.php)
samplefile-Test2.php and (samplefileTest2.php)
...
samplefile-Test-14.php and (samplefileTest14.php)
...
So that require replacing empty space with '-', or trimming empty space at all. Is it possible to achieve what I want? If yes how do I do this? Thank you very much for your help in advance!
UPDATE: I can do this, but that's not good enough, hoping to do better (:
RewriteCond %{QUERY_STRING} ^post=(.*)$
RewriteRule ^samplefile\.php$ http://www.exampledomain.com/? [R=301,L]
Upvotes: 1
Views: 201
Reputation: 785651
You can use these 2 distinct set of rules:
For 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]
For 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]
Upvotes: 1