2016-08-22 139 views
2

我有一个与htaccess重定向的问题。 我需要将3个不同的路径重定向到3个不同的页面,我有静态页面,如果用户是爬虫。重定向htaccess不能正常工作

实施例:

  • www.example.com - > www.example.com/static/index.php
  • www.example.com/news - > www.example.com/static/ news.php
  • www.example.com/home - > www.example.com/static/home.php

我尝试了以下配置,但我认为有冲突的一些规则,其实我仍然被重定向到预渲染服务。

<IfModule mod_rewrite.c> 
    Options +FollowSymlinks 
    RewriteEngine On 

    RewriteRule ^static - [L,NC] 

    RewriteCond %{HTTP_USER_AGENT} bot|crawl|slurp|spider|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|bingbot|Baiduspider|Yahoo|YahooSeeker|quora\ link\ preview|showyoubot|outbrain|pinterest|applebot [NC,OR] 
    RewriteCond %{REQUEST_URI} ^/home$ 
    # Proxy the request  
    RewriteRule ^/home /static/home.php [L] 

    RewriteCond %{HTTP_USER_AGENT} bot|crawl|slurp|spider|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|bingbot|Baiduspider|Yahoo|YahooSeeker|quora\ link\ preview|showyoubot|outbrain|pinterest|applebot [NC,OR] 
    RewriteCond %{REQUEST_URI} ^/news$ 
    # Proxy the request  
    RewriteRule ^/news /static/news.php [L] 

    RewriteCond %{HTTP_USER_AGENT} bot|crawl|slurp|spider|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|bingbot|Baiduspider|Yahoo|YahooSeeker|quora\ link\ preview|showyoubot|outbrain|pinterest|applebot [NC,OR] 
    RewriteCond %{REQUEST_URI} ^/$ 
    # Proxy the request  
    RewriteRule ^/$ /static/index.php [L] 

    # Handle Prerender.io 
    RequestHeader set X-Prerender-Token "------------------" 
    RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|bingbot|Baiduspider|Yahoo|YahooSeeker|quora\ link\ preview|showyoubot|outbrain|pinterest|applebot [NC,OR] 
    RewriteCond %{QUERY_STRING} _escaped_fragment_ 
    # Proxy the request 
    RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) http://service.prerender.io/http://%{HTTP_HOST}/$2 [L] 


    # (REQUEST_FILENAME is only relative in virtualhost context, so not usable) 
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] 
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d 
    # Go to it as is 
    RewriteRule^- [L] 

    # If path ends with/and is not just a single /, redirect to without the trailing/
    RewriteCond %{REQUEST_URI} ^.*/$ 
    RewriteCond %{REQUEST_URI} !^/$ 
    RewriteRule ^(.*)/$ $1 [R,QSA,L] 

    # Accept everything on index.html 
    RewriteRule^index.html [L] 
</IfModule> 

我使用谷歌浏览器正确更改了我的用户代理,但我仍然被重定向到预渲染服务。

+0

删除斜线开头,例如'^重写规则回家/static/home.php [L]' –

+1

......在每个目录.htaccess文件, 'RewriteRule' _pattern_与URL路径匹配的不是目录前缀(.htaccess文件所在位置) - 因此它从不以斜杠开始。 – MrWhite

回答

2

有这样说:

自该模式
Options +FollowSymlinks 
RewriteEngine On 

RewriteCond %{HTTP_USER_AGENT} bot|crawl|slurp|spider|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|bingbot|Baiduspider|Yahoo|YahooSeeker|quora\ link\ preview|showyoubot|outbrain|pinterest|applebot [NC] 
# Proxy the request  
RewriteRule ^(home|news)(/.*)?$ /static/$1.php [L,NC] 

RewriteCond %{HTTP_USER_AGENT} bot|crawl|slurp|spider|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|bingbot|Baiduspider|Yahoo|YahooSeeker|quora\ link\ preview|showyoubot|outbrain|pinterest|applebot [NC,OR] 
# Proxy the request  
RewriteRule ^/?$ /static/index.php [L] 

# If path ends with/and is not just a single /, redirect to without the trailing/
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)/$ /$1 [NE,R=302,L] 

# Handle Prerender.io 
RequestHeader set X-Prerender-Token "------------------" 
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|bingbot|Baiduspider|Yahoo|YahooSeeker|quora\ link\ preview|showyoubot|outbrain|pinterest|applebot [NC,OR] 
RewriteCond %{QUERY_STRING} _escaped_fragment_ 
# Proxy the request 
RewriteRule ^(?!.*?\.(js|css|xml|less|png|jpe?g|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpe?g||tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff))(.*) http://service.prerender.io/http://%{HTTP_HOST}/$2 [L] 

# (REQUEST_FILENAME is only relative in virtualhost context, so not usable) 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d 
# Go to it as is 
RewriteRule^- [L] 

# Accept everything on index.html 
RewriteRule^index.html [L] 
+1

谢谢!一切正常。 – StarsSky