Forum Moderators: phranque
I have the following in my .htaccess file
# Static links are processed by my dynamic backend script
RewriteRule ^directory/(.*)/$ /file2/page.cgi?template=special.html&keyword=$1
...now that displays the results that I need however the resulting URL is
[website.com...]
depending on how many words was in the original URL. The link URL that may contain more than one word with spaces is generated by a program so I currently am unable to remove the spaces. I also don't know how many words there might be and correspondingly how many spaces. How do I get rid of the %20's and replace with - dashes? without sending the dashed words to the file2page.cgi?template=special.html$keyword=$1
? Thanks ahead for your help
> The link URL that may contain more than one word with spaces is generated by a program...
If your spaced URL links are output by your script, and your purpose is to change them before they are displayed in a browser or in search engine results, then mod_rewrite won't help you. mod_rewrite changes URLs after they are *received* in requests sent to your server by clients. It can then modify the URL for use inside the server, or generate an external redirect to a different URL.
While this latter function may sound like what you need, the problem is that your script will continue to generate spaced URLs, and this will confuse search engines and may lead to problems. On wholly-dynamic sites, the place to fix URLs is in the script that generates them. After doing so, you can use mod_rewrite to externally redirect old cached and bookmarked URLs to the new ones.
If you cannot edit your script, another approach is to add a 'wrapper' script around it. In use, you call the wrapper instead of the original script. The wrapper then modifies STDOUT so that it can intercept the original script's output, grabs that script's output, filters it, and changes the URLs to the form you want. This is hugely inefficient, though.
Jim
You are correct. I got the script changed so now the URL's have dashes. One challenge remains that needs a slight mod to my existing mod rewrite (.htaccess is located in the cgi-bin):
RewriteCond %{QUERY_STRING} keywords=(.*)
RewriteRule ^folder1/file1.cgi /a/%1/? [R=301,L]
Eg. If a search engine has the following old page cached like this:
[website.com...]
...then the above rewrite works perfectly at redirecting that page request to the following
[website.com...]
I need it to be redirected in the exact same way but just take out the spaces and put in dashes, however many spaces there might be to show this:
[website.com...]
RewriteCond %{QUERY_STRING} keywords=(.*)
RewriteRule ^folder1/file1\.cgi$ spaced/a/%1/?
RewriteRule ^spaced/a/(.+)\%20(.+)\%20(.+)\%20(.+)$ spaced/a/%1-%2-%3-%4
RewriteRule ^spaced/a/(.+)\%20(.+)\%20(.+)$ spaced/a/%1-%2-%3
RewriteRule ^spaced/a/(.+)\%20(.+)$ spaced/a/%1-%2
RewriteRule ^spaced/a/(.+)/$ /a/%1/ [R=301,L]
Jim