大网数据广告活动
网站运营与故障问答

Redirect percent encoded URL in .htaccess

· 52CE 我爱测

Suppose I have the following URL: http://example.com/%D8%B3%DB%8C%D8%B3%D8%AA%D9%85-rss-%D8%B3%D8%A7%DB%8C%D8%AA-%D8%A8%D9%87%DB%8C%D9%86%D9%87-%D8%B4%D8%AF/ How can I redirect this to http://www.example.com ?

问题原文

原帖链接 · PersianGulf · CC BY-SA 3.0

Suppose I have the following URL:

http://example.com/%D8%B3%DB%8C%D8%B3%D8%AA%D9%85-rss-%D8%B3%D8%A7%DB%8C%D8%AA-%D8%A8%D9%87%DB%8C%D9%86%D9%87-%D8%B4%D8%AF/

How can I redirect this to http://www.example.com?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

You can use mod_alias RedirectMatch, for example:

RedirectMatch ^/سیستم-rss-سایت-بهینه-شد/$ http://www.example.com/

Note that the URL-path matched by RedirectMatch is %-decoded.

However, if you are already using mod_rewrite (ie. RewriteRule) for other redirects then you should use mod_rewrite instead to avoid potential conflicts. For example, in .htaccess:

RewriteRule ^سیستم-rss-سایت-بهینه-شد/$ http://www.example.com/ [R,L]

The URL-path matched by the RewriteRule pattern is also %-decoded. But does not start with a slash in per-directory .htaccess files.

Note that these are both temporary (302) redirects. To make them permanent (301) you need to explicitly include the status. ie. RedirectMatch 301 and R=301 respectively.


Match the %-encoded URL-path

If you did specifically want to match against the %-encoded URL (as seen in the request) then you can match against THE_REQUEST server variable using mod_rewrite, for example:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /%D8%B3%DB%8C%D8%B3%D8%AA%D9%85-rss-%D8%B3%D8%A7%DB%8C%D8%AA-%D8%A8%D9%87%DB%8C%D9%86%D9%87-%D8%B4%D8%AF/
RewriteRule ^ http://www.example.com/ [R,L]

THE_REQUEST server variable is not %-decoded before being passed to mod_rewrite.

However, this is now dependent on the requested URL being encoded exactly as stated. It is also marginally less efficient since every request will be processed by the RewriteRule and passed to the preceding condition.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2015-12-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。