2

how to remove %20,:,/,? and many more from url with .htaccess? im already try code from this post, but still not replace/redirect to new url .htaccess url rewrite and removing %20.

this my .htaccess code

 RewriteEngine On RewriteBase / # external redirect from actual URL to pretty one (remove query string) RewriteCond %{THE_REQUEST} \s/+content\.php\?judul=([^\s&]+) [NC] RewriteRule ^ %1? [R=302,L,NE] # convert all space (%20) to hyphen RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [N,NE] RewriteRule "^(\S*) (\S*)$" $1-$2 [L,R=302,NE] # rewrite rule to call actual PHP handler RewriteRule ^([^./]+)\.html$ content.php?judul=$1 [L,QSA,NC] 

my link
http://localhost/web/content.php?judul=Fate/Apocrypha
http://localhost/web/content.php?judul=Isekai%20wa%20Smartphone%20to%20Tomo%20ni

i want "%20" and "/" replace with "-" like this one.
http://localhost/web/content.php?judul=Fate-Apocrypha/
http://localhost/web/content.php?judul=Isekai-wa-Smartphone-to-Tomo-ni/

4
  • From here it looks like you're trying to address the input from the browser and not the output from your web application, %20 is a url encoded space character, don't put spaces in URL notations and they won't encode that way. Commented Jun 24, 2017 at 5:27
  • that link automatic generate from <a href="content.php?judul=<?php echo $value['judul']; ?></a> Commented Jun 24, 2017 at 5:30
  • you can use the php space remove function like this <a href="content.php?judul=<?php echo str_replace(' ','-',$value['judul']); ?>" > </a> Commented Jun 24, 2017 at 9:05
  • then how to rewrite in htaccess, i can replace it with - Commented Jun 24, 2017 at 9:27

2 Answers 2

0

You can add these 2 rules before your last rewrite rule:

# replace %20 and / in QUERY_STRING by hyphen RewriteCond %{QUERY_STRING} "^([^/]*?)(?:/|%20)+([^/]+?(?:/|%20)+.*)$" RewriteRule ^ %{REQUEST_URI}?%1-%2 [N,NE] RewriteCond %{QUERY_STRING} "^([^/]*?)(?:/|%20)+([^/]+?)/?$" RewriteRule ^ %{REQUEST_URI}?%1-%2/ [L,R=302,NE] 
Sign up to request clarification or add additional context in comments.

5 Comments

u can check my web here stanime.pe.hu im tested upload it.. u can see the url by u self, hope u can help me :D im using urldecode and urlencode my .htaccess github.com/jazuly/ahp/blob/master/.htaccess since no one work, im delete all
yeah cause im using urlencode and decode space will replace with "+", but some character like "/" and ":" that convert with %2xx... can i replace it with "-" or "+"?
then how i can rewrite http://stanime.pe.hu/content.php?link=Katsugeki%2FTouken+Ranbu to http://stanime.pe.hu/Katsugeki%2FTouken+Ranbu
Hmm interesting to see a downvote on a working and accepted answer without any comment :)
0

Use this code , hope it will help you.

# remove spaces from start or after / RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L] # remove spaces from end or before / RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L] # replace spaces by - in between RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R] 

NOte- Must add that you need to fix the source of these URLs also because it is really not normal to be getting URLs like this.

7 Comments

stil getting %20 in my url
did you enable rewrite module in htaccess, RewriteEngine On
i try this one work, but displaying not found, content not displaying, RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^%20]*)%20([^\s]*) [NC] <br>RewriteRule ^ /%1-%2 [L,NE,R]
yeah, im already AllowOverride All & uncomment mod_rewrite.so from httpd.conf
try anathor method- replace space before send parameter to url. $yourstring="Isekai-wa-Smartphone-to-Tomo-ni"; $params=str_replace(' ', '-', $yourstring); ?judul=$params
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.