How do I use mod_rewrite to both redirect and rewrite a directory?

痞子三分冷 提交于 2019-12-24 05:53:30

问题


Don't flag this as a duplicate unless you know what you are doing. Some questions look similar, but aren't what I'm looking for.

If a user hits example.com/friendly-folder it should be internally redirected to example.com/system-folder.

However if a user hits the old example.com/system-folder it should be externally redirected to the new name example.com/friendly-folder.

So, basically, the directory URL must always be friendly, but pointing to the old one. I believe if I just made two rules without any condition it would get stuck in an infinite loop.

I've tried this in my .htaccess

RewriteRule ^friendly-folder(.*)$ system-folder$1 [NC,L] 
RewriteCond %{THE_REQUEST} ^GET\ /system-folder
RewriteRule ^system-folder(.*)$ /friendly-folder$1 [R=301,NC,L]

Which effectively internally redirects from the friendly folder to the system one, but doesn't externally redirect from the system folder to the friendly one.

Any ideas? While you are at it, if you have a method that also works for redirecting and rewriting files it would be much appreciated.

Update

Now i've tried this configuration:

RewriteRule ^friendly-folder(.*)$ system-folder$1 [NC,END] 
RewriteRule ^system-folder(.*)$ friendly-folder$1 [R=301,NC,L]

And I'm getting this error log:

[Sun Jan 15 18:40:49.765190 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] strip per-dir prefix: C:/xampp/htdocs/urls/system-folder/ -> system-folder/
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] applying pattern '^friendly-folder(.*)$' to uri 'system-folder/'
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] strip per-dir prefix: C:/xampp/htdocs/urls/system-folder/ -> system-folder/
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] applying pattern '^system-folder(.*)$' to uri 'system-folder/'
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace2] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] rewrite 'system-folder/' -> 'friendly-folder/'
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace3] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] add per-dir prefix: friendly-folder/ -> C:/xampp/htdocs/urls/friendly-folder/
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace2] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] explicitly forcing redirect with http://localhost/C:/xampp/htdocs/urls/friendly-folder/
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace1] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] escaping http://localhost/C:/xampp/htdocs/urls/friendly-folder/ for redirect
[Sun Jan 15 18:40:49.765691 2017] [rewrite:trace1] [pid 3936:tid 1896] mod_rewrite.c(477): [client ::1:50822] ::1 - - [localhost/sid#a92bb8][rid#2a52150/initial] [perdir C:/xampp/htdocs/urls/] redirect to http://localhost/C:/xampp/htdocs/urls/friendly-folder/ [REDIRECT/301]
[Sun Jan 15 18:40:49.771194 2017] [core:error] [pid 3936:tid 1896] (20023)The given path was above the root path: [client ::1:50822] AH00127: Cannot map GET /C:/xampp/htdocs/urls/friendly-folder/ HTTP/1.1 to file

来源:https://stackoverflow.com/questions/41659112/how-do-i-use-mod-rewrite-to-both-redirect-and-rewrite-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!