如何使用mod_rewrite重定向和重写目录?

时间:2017-01-15 08:11:03

标签: php apache .htaccess redirect mod-rewrite

除非您知道自己在做什么,否则不要将此标记为重复。有些问题看似相似,但不是我想要的。

如果用户点击example.com/friendly-folder,则应在内部重定向example.com/system-folder

但是,如果用户点击旧example.com/system-folder,则应将外部重定向更改为新名称example.com/friendly-folder

因此,基本上,目录URL必须始终是友好的,但指向旧的。我相信如果我在没有任何条件的情况下制定了两条规则,它将陷入无限循环。

我在 .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]

哪个在内部将从友好文件夹重定向到系统文件夹,但从外部重定向从系统文件夹到友好文件夹。

有什么想法吗?当你在它的时候,如果你有一个方法也适用于重定向和重写文件,那将非常感激。

更新

现在我尝试了这个配置:

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

我收到此错误日志:

[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

0 个答案:

没有答案