phpBB SEO
Boards
Directory  
SEO  
Downloads
  phpBB SEO : Search Engine Optimization, Directory, Forums  
Index
Forums
Annuaire
Référencement
Télécharger
 
  Search Rechercher
    Register
Username :  Password :  Log me on automatically each visit  
S'enregistrer  
 
   
Replacing underscores with dashes in rewrite rules

 
Post new topic   Reply to topic    phpBB SEO » SEO Forum  » Apache mod Rewrite
::  
Author Message
tbxn



Joined: 03 Aug 2008
Posts: 9

Replacing underscores with dashes in rewrite rulesPosted: Thu Oct 09, 2008 1:04 am    Post subject: Replacing underscores with dashes in rewrite rules

Hello, I was wondering if it's possible to replace underscores with dashes in the mod rewrite rules, and if so how would I go about doing that? I think it's a pretty simple thing to do but I don't know much about the rewrite syntax.

Basically, I have a download script that calls downloaded files like this: site/download.php?file=file_name.zip

I'm trying to rewrite this url to something more friendly, so what I have now is this:

Code:
RewriteRule ^download/(.*)/ site/download.php?file=$1.zip [QSA,L,NC]


That rewrites my url to download/file_name/
All I'd like to do is replace any underscores in filenames with hyphens in the rewritten url.

So site/download.php?file=file_name would become download/file-name/

I'm using ISAPI Rewrite for iis 6 if that matters, although I think it uses most of the same syntax as mod_rewrite.

Thanks for any insight you can offer.
Back to top
dcz
Administrateur - Site Admin
Administrateur - Site Admin


Joined: 28 Apr 2006
Posts: 15242

Replacing underscores with dashes in rewrite rulesPosted: Sat Oct 11, 2008 10:13 am    Post subject: Re: Replacing underscores with dashes in rewrite rules

Well, the rewriterule will not change the urls themselves, but will only allow rewritten urls to work.

So, if you decide to rewrite all your url with hyphens instead of dashes, the same rewriterule will do the job since it's not looking for hyphens or underscores specifically but for any text ( ".*" ).

This means that the "hard" part is to change the way url are rewritten, in your case it seems to mean that you'll need to change the file names and make sure that the db is synchronized if any is used.

By the way, I pretty sure that the QSA flag has no effect in IIS, I don't think you need it in this case, but ...

And a better rewriterule would probably be closer to :
Code:
RewriteRule ^download/([a-z0-9_-]+)/?$ site/download.php?file=$1.zip [L,NC]


Will only match a-z upper and lower case chars, 0-9 numbers, hypens and underscores in file names, if you need exotic chars, you could use :
Code:
[code]RewriteRule ^download/([^/]+)/?$ site/download.php?file=$1.zip [L,NC][/code]


matching anything but "/" as file name. It would be faster and safer to use.

++

_________________
Useful links :
SEO Forum || SEO Directory || SEO phpBB || SEO phpBB3 || Search
____________________

Liens Utiles :
Forum référencement || Annuaire référencement || Référencement phpBB || Référencement phpBB3 || Recherche
Back to top
Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    phpBB SEO » SEO Forum  » Apache mod Rewrite
Page 1 of 1

Navigation Similar Topics

Jump to: