Redirect percent encoded URL in .htaccess
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, apache, 301-redirect, mod-rewrite, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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?
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.
Comments
Post a Comment