5

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?

3

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.

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.