Mobile app version of vmapp.org
Login or Join
Twilah146

: Rewrite rule to replace every %20 in the URL with an underscore I would like my current URLs: http://example.com/files/528/Rabba%20Ho%20-%20Falak%20Shabir%20Song.html To look cleaner like this: http://example.com/files/528/Rabba_Ho

@Twilah146

Posted in: #Htaccess #Seo

I would like my current URLs:
example.com/files/528/Rabba%20Ho%20-%20Falak%20Shabir%20Song.html

To look cleaner like this:
example.com/files/528/Rabba_Ho_-_Falak_Shabir_Song.html

This is my current .htaccess file:

RewriteRule ^category/([^/]*)/([^/]*).html$ /index.php?pid=&ht= [L]
RewriteRule ^category/([^/]*)/([^/]*)/([^/]*).html$ /index.php?pid=&page=&ht= [L]
RewriteRule ^category/([^/]*)/([^/]*)/([^/]*)/([^/]*).html$ /index.php?sort=&pid=&page=&ht= [L]
RewriteRule ^filedownload/([^/]*)/([^/]*)/([^/]*).html$ /fileshow.php?pid=&id=&ht= [L]


How do I modify it to rewrite the encoded spaces and underscores?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Twilah146

2 Comments

Sorted by latest first Latest Oldest Best

 

@Rambettina238

First sorry about my bad english.
I use this in my PHP apps links:

<?php $theURL = "http://example.com/files/528/Rabba Ho - Falak Shabir Song.html"; ?>

<a href="<?= str_replace( " ", "_", $theURL ); ?>" target="_blank">
Rabba Ho - Falak Shabir Song
</a>


EDIT: Sorry for last error, i edit now...

The above code returns a URL like:
example.com/files/528/Rabba_Ho_-_Falak_Shabir_Song.html.

10% popularity Vote Up Vote Down


 

@BetL925

Here is a similar question on StackOverflow: mod_rewrite: replace underscores with dashes

I've modified the answer to replace spaces (decoded %20) with underscore. It works by recursively applying a rewrite rule as long as there are two or more matches, and then once there is only one, a separate rewrite rule does the redirect.

RewriteEngine On
RewriteRule ^(/?files/.*/[^/]*?)s([^/]*?s[^/]*)$ _ [N]
RewriteRule ^(/?files/.*/[^/]*?)s([^/s]*)$ /_ [R=301]


As others have commented, you will need to make sure your application expects underscores in the URL path, and is generating URLs that have the underscore.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme