简体   繁体   English

.htaccess有条件的重写/重定向

[英].htaccess conditional rewrite/redirect

I am having trouble with setting up a redirect. 我在设置重定向时遇到了麻烦。 I have index pages in certain directories, the directory names are simple six character long tokens, like ABCDEF. 我在某些目录中有索引页,目录名是简单的六个字符长的标记,例如ABCDEF。 Names are alphanumeric and all capital letters. 名称是字母数字和所有大写字母。 What I originally wanted was to direct all traffic, lower case and upper case that contains the token to the correct directories. 我最初想要的是将包含令牌的所有流量(小写和大写)定向到正确的目录。 I don't have access to server modules, like mod_speling, therefore I use a php script to do the actual url rewrite. 我无权访问服务器模块,例如mod_speling,因此我使用php脚本进行实际的URL重写。 It works. 有用。 But then I wanted to add one more step, to direct all traffic like: 但随后我想再增加一个步骤,以引导所有流量,例如:

example.com/ABCDEF
example.com/abcdef
example.com/reference/abcDEF
example.com/referENces/ABCdeF

to the directory: 到目录:

example.com/references/ABCDEF/

I couldn't get these work, my redirect gets in a loop all the time. 我无法完成这些工作,我的重定向一直都处于循环中。

What I have in my htaccess: 我的htaccess中有什么:

# takes care of: example.com/abcdef
RewriteCond %{DOCUMENT_ROOT}/references/%{REQUEST_URI} !-d 
RewriteCond %{REQUEST_URI} ^(/?)([a-zA-Z0-9]{6})(/?)$ 
RewriteRule ^(/?)([a-zA-Z0-9]{6})(/?)$  example.com/rewrite.php?rewrite=example.com%{REQUEST_URI}  [QSA,L,NC,R=302]

# takes care of example.com/reference/abcDEF
RewriteCond %{REQUEST_URI} ^(/?)([REFNCS?refncs?]{9})/([a-zA-Z0-9]{6})(/?)$
RewriteRule ^(/?)([REFNCS?refncs?]{9})/([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI}  [QSA,L,NC,R=302]

# this should take care of: example.com/references/abcDEF , but if I uncomment it, we end up in a loop
#RewriteCond %{REQUEST_URI} ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$
#RewriteRule ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI}  [QSA,L,NC,R=302]

# Finally this handles: example.com/ABCDEF
RewriteCond %{DOCUMENT_ROOT}/references/%{REQUEST_URI} -d 
RewriteCond %{REQUEST_URI} ^(/?)([a-zA-Z0-9]{6})(/?)$ 
RewriteRule ^(/?)([a-zA-Z0-9]{6})(/?)$ example.com/references%{REQUEST_URI} [QSA,L,NC,R=302]

At this point I am sure there must be a better, more general regexp solution. 在这一点上,我确信必须有一个更好,更通用的正则表达式解决方案。 The second and third block could be merged somehow. 第二和第三块可以以某种方式合并。

Peter 彼得

Try changing: 尝试更改:

RewriteCond %{REQUEST_URI} ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$
RewriteRule ([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI}  [QSA,L,NC,R=302]

To: 至:

RewriteCond %{REQUEST_URI} ^(\/?)([REFNCSrefncs]{10})\/([a-zA-Z0-9]{6})(\/?)$
RewriteRule ^(\/?)([REFNCSrefncs]{10})\/([a-zA-Z0-9]{6})(\/?)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI}  [QSA,L,NC,R=302]

Looks like this works: 看起来像这样:

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f 
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-d 
RewriteCond %{REQUEST_URI} ^(/?)([REFNCSrefncs]{10})/([a-zA-Z0-9]{6})(/?)$
RewriteRule ^(.*)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI}   [QSA,L,NC,R=302]

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} ^(/?)([REFNCrefnc]{9})/([a-zA-Z0-9]{6})(/?)$
RewriteRule ^(.*)$ example.com/rewrite.php?rewrite=example.com%{REQUEST_URI}  [QSA,L,NC,R=302]

I had to change the order of ([REFNCSrefncs]{10}) and ([REFNCrefnc]{9}). 我必须更改([REFNCSrefncs] {10})和([REFNCrefnc] {9})的顺序。

Thanks for the hints! 感谢您的提示!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM