3

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_-_Falak_Shabir_Song.html

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?

5
  • 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
    Sep 11 '15 at 16:00
  • but through .htacess it can posiible to repalce %20 to undersocre
    – Marvel
    Sep 11 '15 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
    Sep 11 '15 at 16:05
  • i have not proper knowldge in PHP . you can fix it
    – Marvel
    Sep 11 '15 at 16:07
  • Then What Is The Way for Clean url
    – Marvel
    Sep 11 '15 at 16:13
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.

5
  • 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
    Sep 11 '15 at 19:12
  • bro you can Edit all Urls like this
    – Marvel
    Sep 12 '15 at 2:21
  • When I OPen this rewrite egine url this 404 error i got
    – Marvel
    Sep 12 '15 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
    Sep 12 '15 at 11:48
  • but how to i change
    – Marvel
    Sep 12 '15 at 12:41
1

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:

http://example.com/files/528/Rabba_Ho_-_Falak_Shabir_Song.html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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