Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

php - Apache Rewrite Urls With Spaces

I'm currently working on a website built by a previous developer. This website has previously sent links to subscribers with spaces in the url filenames, for example http://website.com/docs/this is a pdf.pdf. I have now fixed these links as they are generated by using php url encoding when the link is generated, because specifically internet explorer users are not finding the file on the linux server because the filename has spaces in it (chrome and FF work by properly inserting %20 into the spaces in the url).

I'm currently trying to find a way to use .htaccess to rewrite these urls that have spaces in them to redirect to the correct url. For example, if a user were to request http://website.com/docs/this is a pdf.pdf the .htaccess will redirect to http://website.com/docs/this%20is%20a%20pdf.pdf. Note that there are thousands of documents on this server so this does need to be dynamic.

WHAT HAVE I TRIED?

I've looked at this and this and more, but can't add more links because SO says I don't have enough reputation.

These options seem to show that it is possible if you know in advance what the filename will be. I need this to be dynamic. Anyone out there have any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put the spaces escaped inside braces, for example..

RewriteRule ^(.+)( )(.+)( )(.+)( )(.+).pdf /$1-$3-$5-$7.pdf

would catch any request for "any four word file.pdf" and rewrite it to /any-four-word-file.pdf

;o) Cor


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...