apache - htaccess 重定向不起作用

标签 apache .htaccess mod-rewrite

我在使用 htaccess 进行重定向时遇到问题。 如果用户是爬虫,我需要将 3 个不同的路径重定向到 3 个不同的页面,其中我有静态页面。

示例:

  • 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>

我使用 Google Chrome 正确更改了我的用户代理,但我仍然被重定向到预渲染服务。

最佳答案

这样吧:

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]

关于apache - htaccess 重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39088805/

相关文章:

regex - 使用 htaccess 将路径目录附加到 url

php - 为什么我的重写规则会禁用额外的 $_GET 数据?

php - 使用 .htaccess 重写 URL(添加查询字符串)

apache - 重写 URL 以重定向并添加前导零

apache - pig apache中列的总和

apache - htaccess 中的 RewriteRule 仅在我直接访问 url 时才起作用

html - 使用 htaccess rewrite 使子目录成为其自己的根目录以用于根相对路径请求

php - 部署新版本应用程序后,我们是否需要重启 apache + APC?

apache - 如何检测文档中的图像

javascript - $routeParams 硬刷新在服务器上不起作用(需要基本标记)