I would like my current URLs:


To look cleaner like this:


This is my current .htaccess file:

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

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

  • This isn't really something you should do (just) in .htaccess. You need to change these URLs in your application. Otherwise, every time a user clicks on one of your internal links (where they will still see the URL that contains %20 ie. spaces) they will be externally redirected, resulting in two requests to your server, which is not desirable.
    – MrWhite
    Commented Sep 11, 2015 at 16:00
  • but through .htacess it can posiible to repalce %20 to undersocre
    – Marvel
    Commented Sep 11, 2015 at 16:02
  • You first need to change the URLs in your application. A lot of people ask a similar question and they get a plain old .htaccess solution. But it's not correct, unless you change the URLs in your application first. The user can still see and copy the %20 from your page.
    – MrWhite
    Commented Sep 11, 2015 at 16:05
  • i have not proper knowldge in PHP . you can fix it
    – Marvel
    Commented Sep 11, 2015 at 16:07
  • Then What Is The Way for Clean url
    – Marvel
    Commented Sep 11, 2015 at 16:13

2 Answers 2


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[^/]*)$ $1_$2 [N]
RewriteRule       ^(/?files/.*/[^/]*?)\s([^/\s]*)$       /$1_$2 [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.

  • I like the idea of the recursive rewrite +1. Slight tweak though... the RewriteRule pattern matches against the %-decoded URL, so %20 should be a literal (escaped) space (eg. \ or \s) in the pattern. Also, the external redirect (2nd rule) needs a slash prefix on the substitution ie. /$1_$2 (or include a RewriteBase / directive), otherwise you get a "broken" redirect in .htaccess.
    – MrWhite
    Commented Sep 11, 2015 at 19:12
  • bro you can Edit all Urls like this
    – Marvel
    Commented Sep 12, 2015 at 2:21
  • When I OPen this rewrite egine url this 404 error i got
    – Marvel
    Commented Sep 12, 2015 at 2:42
  • @Marvel: Yes, you will get a 404, because you also need to change the URLs in your application - as already stated.
    – MrWhite
    Commented Sep 12, 2015 at 11:48
  • but how to i change
    – Marvel
    Commented Sep 12, 2015 at 12:41

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

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

The above code returns a URL like:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.