2017-02-23 57 views
0

我正在一个网站上工作,我有很多短网址被重定向到更大版本的URL。这个想法是,短网址是离线分发的,用户通常会在浏览器地址栏中记住它们和类型。例如: http://www.example.com/abc应重定向到http://www.example.com/higher_education/companion/politics/abc.html
此外还有一些外部链接。这样的:http://www.example.com/google将用户重定向到https://www.google.com
编辑 - 还有另一种方案,其中http://www.example.com/elementary/def.html将被重定向到http://www.example.com/elementary/xyz.html
然而,我们现在想RewriteMap实现这一目标。我们已经为编写的代码如下:httpd.conf中的RewriteMap和.htaccess冲突中的RewriteRule

<IfModule rewrite_module> 
RewriteEngine on 
RewriteMap custommap txt:/var/www/vhosts/example.com/httpdocs/map.txt 
#RewriteCond %{REQUEST_URI} !^/index.php$ 
RewriteRule "^(.*)$" "${custommap:$0}" [R,L,NC] 
</IfModule> 

map.txt包含:

abc http://www.example.com/higher_education/companion/politics/abc.html 
google https://www.google.com 

问题是.htaccss文件还包含了一些重写规则。

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/(.*)\.html$ /index.php?sid=$1&ssid=$2 [L,NC,QSA] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)\.html$ /index.php?sid=$1 [L,NC,QSA] 

所以任何要求自带.htmlindex.php被用作标准品,CMS功能。但是,现在代码进入重定向循环。 如果有人分享一些光线以缓解这个问题,那就太好了。 Apache版本是2.2,所以我不能使用任何IF-ELSE。

问候,
Aneek

回答

0

你应该把一些限制规则从RewriteMap服务网址:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond ${custommap:$0} !^$ 
RewriteRule ^[^.]+$ ${custommap:$0} [R=301,L,NE] 

这将确保没有DOT在URI唯一的非文件和非目录将由custommap处理。

更重要的是它还会在重定向之前检查custommap中是否存在映射。

确保在测试此更改之前清除浏览器缓存。

+0

嗨,我试过这个,是的,这个解决方案没有文件扩展名。但是,这给了我另一个挑战,如果我有一个需要重定向的URL,我该怎么办? –

+0

'RewriteRule。+ $ {custommap:$ 0} [R = 301,L,NE]'也应该可以正常工作,但我真的不明白为什么在映射文件的键名中不能避免DOT。 – anubhava

+0

谢谢,我也会检查这一个。 –