如何使用.htaccess从网址中删除%20 [英] How to remove %20 from url with .htaccess

查看:110
本文介绍了如何使用.htaccess从网址中删除%20的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除%20,:,/,?还有更多来自.htaccess的url? 我已经尝试过这篇文章中的代码,但仍然不能替换/重定向到新的URL .htaccess网址重写并删除%20 .

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.

这是我的.htaccess代码

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]

我的链接
http://localhost/web/content.php?judul = Fate/Apocrypha
http://localhost/web/content.php? judul = Isekai%20wa%20Smartphone%20to%20Tomo%20ni

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

我希望这样将%20"和"/"替换为-".
http://localhost/web/content.php?judul = Fate-Apocrypha/
http://localhost/web/content.php ?judul = Isekai-wa-Smartphone-to-Tomo-ni/

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/

推荐答案

您可以在最后一个重写规则之前添加以下2条规则:

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]

这篇关于如何使用.htaccess从网址中删除%20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆